C# – NamedPipeClientStream can not access to NamedPipeServerStream under session 0

cnamed-pipeswindows 7

I have NamedPipeClientStream which connects to NamedPipeServerStream. They exchange a couple of messages, and then NamedPipeClientStream closing, while NamedPipeServerStream recreated and continue listening for the client pipes. (I couldn't make a working async Server Pipe, so this is some kind of dog-nail)

The client-server interaction works fine during my client's streams launched from normal user sessions.

But there are a situation when Client pipe is launched from session 0 on Win7 and win2008 server. When this happens I had an error in Client stream:

"Access to the path is denied"

What is the problem? How to avoid it?

Sorry I can't tell you more info about exception. Only I have is this message in log. And I can't debug my program from zero session, can I?

The server stream code:

PipeSecurity ps = new PipeSecurity();
System.Security.Principal.SecurityIdentifier sid = new System.Security.Principal.SecurityIdentifier(System.Security.Principal.WellKnownSidType.BuiltinUsersSid, null);
PipeAccessRule par = new PipeAccessRule(sid, PipeAccessRights.ReadWrite, System.Security.AccessControl.AccessControlType.Allow);
ps.AddAccessRule(par);
pipeClientConnection = new NamedPipeServerStream(General.PIPENAME, PipeDirection.InOut, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous, General.BUFFERSIZE, General.BUFFERSIZE, ps);
Console.Write("Waiting for client connection...");
IAsyncResult result = pipeClientConnection.BeginWaitForConnection(OnPipeConnected, pipeClientConnection);

Maybe something is wrong with security settings?

And the client code:

using (NamedPipeClientStream pipeStream = new NamedPipeClientStream(".", General.PIPENAME, PipeDirection.InOut))
{
    try
    {
        Console.WriteLine("Connecting with pipe...");
        pipeStream.Connect(General.CONNECTIONTIMEOUT);
        Console.WriteLine("Pipe connection established");
        //..do something..
    }
    //...
}

The server is launched as windows service under LocalSystem. The client – is a simple console application. It's launched by another application launched from LocalSystem service.

Best Answer

Looks like the problem was in security settings here:

System.Security.Principal.SecurityIdentifier sid = new System.Security.Principal.SecurityIdentifier(System.Security.Principal.WellKnownSidType.BuiltinUsersSid, null);

Should be :

System.Security.Principal.SecurityIdentifier sid = new System.Security.Principal.SecurityIdentifier(System.Security.Principal.WellKnownSidType.WorldSid, null);

Thanks microsoft communnity