简体   繁体   English

NamedPipeServerStream 未授权访问异常

[英]NamedPipeServerStream Unauthorized Access exception

I recieved an Event Viever entry from one of my users saying that Unauthorized Access Exception happened.我从我的一位用户那里收到了一个 Event Viever 条目,说发生了未经授权的访问异常。 The process is started from a service as a SYSTEM.该过程从作为 SYSTEM 的服务启动。 I was able to narrow down the error to the part where NamedPipeServerStream is created:我能够将错误缩小到创建 NamedPipeServerStream 的部分:

  // <error in this block>
  PipeSecurity pipeSa = new PipeSecurity(); 
  pipeSa.AddAccessRule(new PipeAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), PipeAccessRights.ReadWrite, AccessControlType.Allow));
  NamedPipeServerStream np = new NamedPipeServerStream("streamname", PipeDirection.InOut,1,PipeTransmissionMode.Message,PipeOptions.None,16383,1, pipeSa);
  // </error in this block>


  while (true){
     try{
        if (!np.IsConnected)
           np.WaitForConnection();
     }catch(Exception e){

     }
  }

I am pretty sure that the error is in the 3 line block above.我很确定错误在上面的 3 行块中。 It doesn't happen on any of my computers so I can't repeat the error.它不会发生在我的任何计算机上,所以我不能重复错误。 What in the above code is not considered best practice and can cause errors?上述代码中的哪些内容不被视为最佳实践并且可能导致错误?

error message image:错误信息图片:

在此处输入图像描述

The question you asked has very few details.你问的问题细节很少。 So it will be hard to narrow down what the true cause is.因此,很难确定真正的原因是什么。 However I'm willing to take a stab at it.不过我愿意试一试。

If you look at the documentation for the Unauthorized Access Exception如果您查看未授权访问异常的文档

The exception that is thrown when the operating system denies access because of an I/O error or a specific type of security error.当操作系统由于 I/O 错误或特定类型的安全错误而拒绝访问时引发的异常。

We can see that your code already swallows all exception.我们可以看到您的代码已经吞噬了所有异常。 So there for we can probably assume that this is thrown while creating the pipe.所以我们可以假设这是在创建 pipe 时抛出的。

My best guess is that there are times that you cannot access the pipe, because system does not have the proper access to the pipe.我最好的猜测是,有时您无法访问 pipe,因为系统无法正确访问 pipe。 After doing some quick research.在做了一些快速研究之后。 This question seems to have a lot of good information worth the try.这个问题似乎有很多值得一试的好信息。 To Paraphrase a few answers:转述几个答案:

There are two things which can cause the instantiation of a second or subsequent NamedPipeServerStream on the same pipe to fail:有两件事会导致在同一 pipe 上实例化第二个或后续 NamedPipeServerStream 失败:

  • the maxNumberOfServerInstances ctor argument must have been set to more than 1 when the first instance of the pipe server was created.创建 pipe 服务器的第一个实例时,maxNumberOfServerInstances ctor 参数必须设置为大于 1。 If not, the second call will fail unless the first instance has already been closed completely.如果没有,第二次调用将失败,除非第一个实例已经完全关闭。
  • the process calling the ctor must have the access right represented by PipeAccessRights.CreateNewInstance.调用 ctor 的进程必须具有由 PipeAccessRights.CreateNewInstance 表示的访问权限。 This is a powerful right which the pipe server should guard jealously, as it allows its possessor the ability to act as a pipe server.这是 pipe 服务器应该警惕的强大权利,因为它允许其拥有者充当 pipe 服务器的能力。

You're probably missing a privilege's you need to add an Access Right perhaps you need to provide control to the system:您可能缺少需要添加访问权限的权限,也许您需要为系统提供控制:

pipeSa.AddAccessRule(new PipeAccessRule("SYSTEM", PipeAccessRights.FullControl, AccessControlType.Allow));
pipeSa.AddAccessRule(pa);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM