我已经使用System.IO.Pipes
创建了一个命名管道。它的工作很好,直到我不得不运行程序在管理模式。当提升时,客户端无法再连接(客户端没有运行提升)。如果我以管理员身份运行客户端,则它连接良好,因此它看起来像是权限问题。我一直在研究如何解决这个问题,但没有成功(我发现处理Windows安全问题令人难以置信)。我的目标是允许任何客户端--无论是否提升--能够连接到管道。
我更改的第一件事是打开具有访问权限的管道:
pipeServer = new NamedPipeServerStream(pipeName,
PipeDirection.InOut,
1,
PipeTransmissionMode.Message,
PipeOptions.Asynchronous,
0x4000,
0x400,
null,
HandleInheritability.Inheritable,
PipeAccessRights.ChangePermissions | PipeAccessRights.AccessSystemSecurity);
然后我把这段代码拼凑在一起。所有操作都会运行,直到SetEntriesInAcl
调用失败为止:
错误: 0x534 “帐户名称和安全ID之间没有进行映射。”
IntPtr ownerSid = IntPtr.Zero;
IntPtr groupSid = IntPtr.Zero;
IntPtr dacl = IntPtr.Zero, newDacl = IntPtr.Zero;
IntPtr sacl = IntPtr.Zero;
IntPtr securityDescriptor = IntPtr.Zero;
if (SUCCEEDED(GetSecurityInfo(pipeServer.SafePipeHandle.handle.DangerousGetHandle(),
SE_OBJECT_TYPE.SE_KERNEL_OBJECT,
SECURITY_INFORMATION.DACL_SECURITY_INFORMATION,
out ownerSid,
out groupSid,
out dacl,
out sacl,
out securityDescriptor))) {
EXPLICIT_ACCESS ea = new EXPLICIT_ACCESS();
BuildExplicitAccessWithName(ref ea, "Everyone", GENERIC_ALL, ACCESS_MODE.GRANT_ACCESS, NO_INHERITANCE);
// Next line fails
if (SUCCEEDED(SetEntriesInAcl(1, ref ea, dacl, out newDacl))) {
uint retval = SetSecurityInfo(handle,
SE_OBJECT_TYPE.SE_KERNEL_OBJECT,
SECURITY_INFORMATION.DACL_SECURITY_INFORMATION,
IntPtr.Zero,
IntPtr.Zero,
newDacl,
IntPtr.Zero);
// Haven't reached this point yet
}
}
BuildExplicitAccessWithName
函数不返回值,但似乎成功了。这就是电话之后的样子:
我希望能在这里提供任何帮助。
(所有Win32函数和数据类型都在pinvoke.net上找到。另外,我使用的是Windows 10。)
发布于 2018-07-27 13:41:36
我最终不用使用任何本地电话。PipeSecurity
类起作用了。诀窍是我必须将它传递给构造函数:
// Creates a PipeSecurity that allows users read/write access
PipeSecurity CreateSystemIOPipeSecurity()
{
PipeSecurity pipeSecurity = new PipeSecurity();
var id = new SecurityIdentifier(WellKnownSidType.AuthenticatedUserSid, null);
// Allow Everyone read and write access to the pipe.
pipeSecurity.SetAccessRule(new PipeAccessRule(id, PipeAccessRights.ReadWrite, AccessControlType.Allow));
return pipeSecurity;
}
在创建管道时使用该函数:
PipeSecurity pipeSecurity = CreateSystemIOPipeSecurity();
pipeServer = new NamedPipeServerStream(pipeName,
PipeDirection.InOut,
1,
PipeTransmissionMode.Message,
PipeOptions.Asynchronous,
0x4000,
0x400,
pipeSecurity,
HandleInheritability.Inheritable);
https://stackoverflow.com/questions/51546328
复制相似问题