命名管道客户端无法连接到作为网络服务运行的服务器

Col*_*nee 7 c# windows permissions windows-services named-pipes

我有一个在网络服务帐户下运行的服务.该服务只是设置一个命名管道并侦听连接:

NamedPipeServerStream listeningPipe = new NamedPipeServerStream("ourservicepipe",
    PipeDirection.InOut, NamedPipeServerStream.MaxAllowedServerInstances, 
    PipeTransmissionMode.Message, PipeOptions.Asynchronous);
listeningPipe.BeginWaitForConnection(OnPipeConnected, listeningPipe);
Run Code Online (Sandbox Code Playgroud)

我有一个应用程序在同一台机器上的标准用户帐户上运行.它尝试连接到服务创建的命名管道:

NamedPipeClientStream pipe = new NamedPipeClientStream(".", "ourservicepipe", 
    PipeDirection.InOut, PipeOptions.Asynchronous);
pipe.Connect(2000);
pipe.ReadMode = PipeTransmissionMode.Message;
Run Code Online (Sandbox Code Playgroud)

当我打电话时Connect,最终会抛出一个InvalidOperationException.如果我在同一个用户上运行该服务,它连接正常.如果我以管理员身份运行客户端,它连接正常.这让我相信问题在于权限,但我不知道在安装过程中需要设置哪些权限.

如何允许客户端连接到服务器,而不要求客户端以管理员身份运行?

小智 13

使用默认服务DACL创建管道服务器,以便只有管理员或系统用户可以连接到管道.您需要使用适当的访问规则设置管道服务器,以使所有客户端连接成功.下面是设置访问规则以访问每个人访问管道的代码:

    PipeSecurity pipeSa = new PipeSecurity(); 
    pipeSa.SetAccessRule(new PipeAccessRule("Everyone", 
                    PipeAccessRights.ReadWrite, AccessControlType.Allow)); 
    listeningPipe.SetAccessControl(pipeSa);
Run Code Online (Sandbox Code Playgroud)

最好只定义一组最小的用户来访问管道服务器以使其安全.

  • 试图执行未经授权的操作 - pipe.SetAccessControl(pipeSa); (5认同)

Roe*_*lVB 6

我不允许发表评论,但我想指出dvansanth的答案是正确的,具体取决于操作系统语言.当操作系统语言不是英语时,"Everyone"组可能不存在.

我已经解决了这个问题:

PipeSecurity pipeSa = new PipeSecurity();
pipeSa.SetAccessRule(new PipeAccessRule(new SecurityIdentifier(WellKnownSidType.AuthenticatedUserSid, null), 
                PipeAccessRights.ReadWrite, AccessControlType.Allow)); 
listeningPipe.SetAccessControl(pipeSa);
Run Code Online (Sandbox Code Playgroud)

请注意,这是针对"经过身份验证的用户"而不是"所有人"