简体   繁体   English

客户端登录时如何在服务器上的网络服务帐户中触发服务?

[英]How to trigger a Service in a Network Service Account on a server when a client logs in?

We have an existing system in which a windows service, which is network service enabled, is hosted on a network service account on the server. 我们有一个现有系统,其中已启用网络服务的Windows服务托管在服务器上的网络服务帐户上。 There are windows services installed on every client which start automatically once a user logs in through each client and these services on client trigger the service on the server telling it that for eg client A has logged in. 在每个客户端上安装了Windows服务,一旦用户通过每个客户端登录,它们就会自动启动,并且客户端上的这些服务会触发服务器上的服务,告知它例如客户端A已登录。

What i want to do is create a network service and host it on the server, and trigger it directly without installing a separate windows service on each client. 我想要做的是创建一个网络服务并将其托管在服务器上,并直接触发它,而无需在每个客户端上安装单独的Windows服务。 Is it possible? 可能吗? I want each client to use the existing network service and inform it upon log-in that it is online. 我希望每个客户端使用现有的网络服务,并在登录时通知它在线。 Sort of maintaining a user log-in log at the server with time. 可以随时维护服务器上的用户登录日志。

I have made this using Simple Impersonation Library . 我已经使用简单模拟库进行了此操作。 Here is the snippet that I use on my WPF client. 这是我在WPF客户端上使用的代码段。

public static async Task ToggleServiceStatus(this EdiServiceInfo serviceInfo)
        {
            await Task.Factory.StartNew(() =>
            {
                using (
                    Impersonation.LogonUser(serviceInfo.Domain, serviceInfo.User, serviceInfo.Pswd,
                        Environment.MachineName.Equals(serviceInfo.ComputerName,
                            StringComparison.InvariantCultureIgnoreCase)
                            ? LogonType.Network
                            : LogonType.Interactive))
                {
                    var service = new ServiceController(serviceInfo.ServiceName, serviceInfo.ComputerName);
                    if (service.Status == ServiceControllerStatus.Stopped)
                    {
                        service.Start();
                        service.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromSeconds(60));
                    }
                    else
                    {
                        service.Stop();
                        service.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(60));
                    }
                }
            });
        }

This is useful if you want to trigger the service from your client. 如果要从客户端触发服务,这将很有用。 But if the service is already working and just want to respond to log in, then you could maintain a table of clients with a flag DidLoggedIn, and your windows service should have a long running task monitoring this flags. 但是,如果该服务已经在工作并且只想响应登录,那么您可以维护一个带有DidLoggedIn标志的客户端表,并且Windows服务应该有长时间运行的任务来监视此标志。 The idea here is that whenever the client logs in you will set the flag to true, and on your server side the windows service's long running task will detect this and well do whatever you want it to do. 这里的想法是,每当客户端登录时,您都将标记设置为true,并且在服务器端,Windows服务的长期运行任务将检测到该错误,并做好您想要执行的任何操作。

Hope it helps 希望能帮助到你

EDIT>>> I have an application that does so. 编辑>>>我有一个这样做的应用程序。 Let me share some snippets. 让我分享一些摘要。

public virtual async Task InitAsync()
        {
            EdiDataAccess.ResetServiceFlags();
            EdiDataAccess.SetServiceAsWorking();
            var startMonitorTask = new Func<Task>(StartAllWorkersAsync)
                .CyclicalTask(TimeSpan.FromSeconds(MonitorSeconds), MonitorCancellationToken);
            var stopMonitorTask = new Func<Task>(StopRequestsMonitor)
                .CyclicalTask(TimeSpan.FromSeconds(MonitorSeconds), MonitorCancellationToken);
            await TaskEx.WhenAll(startMonitorTask, stopMonitorTask);
        }

startMonitorTask is the long running task to monitor the flags. startMonitorTask是长时间运行的任务,用于监视标志。 CyclicalTask is just an extension method of my own: CyclicalTask只是我自己的扩展方法:

public static Task CyclicalTask(this Func<Task> task, TimeSpan waitTimeSpan,
            CancellationToken token = default(CancellationToken))
        {
            return TaskEx.Run(async () =>
            {
                while (true)
                {
                    token.ThrowIfCancellationRequested();
                    await task();
                    token.WaitHandle.WaitOne(waitTimeSpan);
                }
            }, token);
        }

StartAllWorkersAsync is the method than has the monitoring funcionality. StartAllWorkersAsync是具有监视功能的方法。

protected virtual async Task StartAllWorkersAsync()
        {
            var ediCustomersToStart = EdiDataAccess.GetEdiCostumersToStart();
            var ediTasks = ediCustomersToStart
                .Select(StartWorkerAsync)
                .ToList();
            await TaskEx.WhenAll(ediTasks);
        }

Where EdiDataAccess.GetEdiCostumersToStart(); 其中EdiDataAccess.GetEdiCostumersToStart(); retrieves all those customers that have requested to log in 检索所有要求登录的客户

This my window service can detect when a customer has requested to begin a session. 我的窗口服务可以检测到客户何时请求开始会话。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何授予对NETWORK SERVICE帐户的SQL访问权限? - How to give SQL access to NETWORK SERVICE account? SMTP 服务器需要安全连接或客户端未通过服务帐户身份验证 - The SMTP server requires a secure connection or the client was not authenticated for service account 从外部服务器客户端点击内部网络恢复服务 - Hit Internal network rest service from external server client side 命名管道客户端无法连接到作为网络服务运行的服务器 - Named pipe client unable to connect to server running as Network Service 如何获得内置Windows“网络服务”帐户的本地化版本? - How to get localized version of built-in windows 'Network Service' account? 在网络服务帐户下访问 Windows 共享 - Access Windows Share under Network Service account WCF服务:如何查找服务器日志以了解错误? - WCF Service: How to find server logs to understand error? C#如何禁用本地用户帐户的交互式登录。 与网络服务帐户类似 - C# How do I disable interactive login for a local user account. Similar to Network Service account Windows服务Process.Start不在网络服务帐户下工作 - Windows service Process.Start not working under network service account Windows服务无法访问网络服务帐户以访问msmq - windows service has access denied to the Network Service account to msmq
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM