代码之家  ›  专栏  ›  技术社区  ›  Sunnygurke

WindowsService和NamedPipes:未经授权的访问异常

  •  1
  • Sunnygurke  · 技术社区  · 12 年前

    当我试图从WPF测试应用程序访问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

    提前感谢!

    1 回复  |  直到 12 年前
        1
  •  1
  •   Sunnygurke    12 年前

    好我成功了。

    解决方案:

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