C# WindowsService和NamedPipes:UnauthorizedAccessException

C# WindowsService和NamedPipes:UnauthorizedAccessException,c#,.net,windows-services,named-pipes,C#,.net,Windows Services,Named Pipes,当我尝试从WPF Testapp访问NamedPipeServer(作为WindowsService运行)时,总是会收到UnauthorizedAccessException 服务器: class PipeConnector { private volatile bool _shouldStop; NamedPipeServerStream pipeServer; public PipeConnector() {

当我尝试从WPF Testapp访问NamedPipeServer(作为WindowsService运行)时,总是会收到UnauthorizedAccessException

服务器:

 class PipeConnector
    {
        private volatile bool _shouldStop;
        NamedPipeServerStream pipeServer;
        public PipeConnector()
        {
            _shouldStop = false;
            PipeSecurity ps = new PipeSecurity();
            ps.AddAccessRule(new PipeAccessRule(WindowsIdentity.GetCurrent().Name, PipeAccessRights.ReadWrite, AccessControlType.Allow));
            pipeServer = new NamedPipeServerStream("ConnectorPipe", PipeDirection.InOut, 10,
                                    PipeTransmissionMode.Message,
                                    PipeOptions.WriteThrough, 4028, 4028, ps);    
        }

        public void Start()
        {
            pipeServer.WaitForConnection();

            StreamWriter sw = new StreamWriter(pipeServer);

            while (!_shouldStop)
            {
                sw.WriteLine("Here is Server, I'm working");
                sw.Flush();
                Thread.Sleep(5000);
            }
            this.pipeServer.Close();
        }

        public void RequestStop()
        {
            this._shouldStop = true;
            this.pipeServer.Close();
        }
    }
客户:

public delegate void Notification(string text);
    class PipeConnector
    {

        NamedPipeClientStream pipeClient;
        public Notification notify;
        public PipeConnector()
        {
             pipeClient =
                    new NamedPipeClientStream(".", "ConnectorPipe",
                        PipeDirection.InOut);
        }

        public void Start()
        { 
            pipeClient.Connect();
            StreamReader sr = new StreamReader(pipeClient);

            while (true)
            {
                string text = sr.ReadLine();
                notify(text);
            }

        }
    }
我为类似的问题找到了很多解决方案,但没有人为我工作

我也试过:


提前谢谢

嗯。。我让它工作了

解决方案:


我使用了新的安全标识符(WellKnownSidType.BuiltinUsersSid,null),而不是
windowsintity.GetCurrent().Name
。。我让它工作了

解决方案:

我使用了新的安全标识符(WellKnownSidType.BuiltinUsersSid,null)