Sunnygurke
Sunnygurke

Reputation: 117

WindowsService and NamedPipes: UnauthorizedAccessException

When I try to access a NamedPipeServer (Running as a WindowsService) from a WPF-Testapp i always get an UnauthorizedAccessException.

Server:

 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();
        }
    }

Client:

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);
            }

        }
    }

I found alot solutions for similar problems, but no one worked for me..

I also tried this: http://support.microsoft.com/kb/126645/en-us

Thanks in Advance!

Upvotes: 1

Views: 1290

Answers (1)

Sunnygurke
Sunnygurke

Reputation: 117

Well.. I got it working.

The Solution:

Instead of WindowsIdentity.GetCurrent().Name i used new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null)

Upvotes: 1

Related Questions