1

当我尝试从 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);
            }

        }
    }

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

我也试过这个:http: //support.microsoft.com/kb/126645/en-us

提前致谢!

4

1 回答 1

1

嗯..我得到它的工作。

解决方案:

而不是WindowsIdentity.GetCurrent().Name我用new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null)

于 2013-03-07T12:18:36.197 回答