关于在UWP中使用管道IPC的一些复杂问题(不要告诉我要使用应用程序服务,这对于我的项目来说解决方案的延迟太低了)。
我正试图在我的两个项目之间获得工控机,其中一个是UWP打包应用程序,另一个是C# .net6应用程序。我已经在我的NamedPipeServerStream程序中成功地创建了一个.net6。最初,由于“拒绝对路径的访问”错误,我的UWP侧管道客户端(NamedPipeClientStream)在创建过程中会失败。
做研究,我已经推断,我需要添加一些与我的UWP软件包的SID相关的访问规则,这样我的UWP应用程序就可以成功地与我的外部应用程序通信。看起来普通的.net NamedPipeServerStream不允许通过构造函数添加访问规则,但是我能够使用一个nuget包,NamedPipeServerStream.NetFrameworkVersion来解决这个问题。我能够使用文档中描述的函数获得一个SID,尽管结果证明这是错误的SID。使用powershell命令"CheckNetIsolation.exe LoopbackExempt -s“中的硬编码SID,我成功地在我的UWP应用程序中创建了NamedPipeClientStream对象,通过了拒绝访问的错误。
但是,我直接调用的connect方法没有成功。我最终得到了一个错误:
无法加载文件或程序集'System.Security.Principal.Windows,Version=5.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f7f11d50a3a‘。所定位的程序集的清单定义与程序集引用不匹配。(HRESULT例外: 0x80131040)
有几个问题:
下面是我的一些代码示例:
服务器:
//IntPtr ptr = IntPtr.Zero;
//var result = DeriveAppContainerSidFromAppContainerName("PackageFamilyName", ref ptr);
//var appSid = new SecurityIdentifier(ptr); //results in incorrect SID
var appSid = new SecurityIdentifier("hardCodedSID");
var access = new PipeSecurity();
SecurityIdentifier sid = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
access.AddAccessRule(new PipeAccessRule(sid, PipeAccessRights.FullControl, System.Security.AccessControl.AccessControlType.Allow));
access.AddAccessRule(new PipeAccessRule(sid, PipeAccessRights.AccessSystemSecurity, System.Security.AccessControl.AccessControlType.Allow));
access.AddAccessRule(new PipeAccessRule(appSid, PipeAccessRights.FullControl, System.Security.AccessControl.AccessControlType.Allow));
access.AddAccessRule(new PipeAccessRule(appSid, PipeAccessRights.AccessSystemSecurity, System.Security.AccessControl.AccessControlType.Allow));
pipeServer = NamedPipeServerStreamConstructors.New("testpipe", PipeDirection.InOut, 1, PipeTransmissionMode.Message, PipeOptions.None, 0, 0, access);
pipeServer.WaitForConnection(); // gets past here when client creates NamedPipeClientStream
客户端:
pipeClient = new NamedPipeClientStream(".", "testpipe", PipeDirection.InOut, PipeOptions.Asynchronous);
pipeClient.Connect(5000); // fails here
发布于 2022-03-04 14:18:11
我找到了一个可行的解决方案来做独立的研究。有几个人回答:
客户代码:
var fileName = @"\\.\pipe\\testpipe";
IntPtr pipeHandle = CreateFileW(fileName,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
IntPtr.Zero,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
IntPtr.Zero);
if (pipeHandle.ToInt32() == INVALID_HANDLE_VALUE)
{
var err = $"Failed to create pipe client, win32 error: {Marshal.GetLastWin32Error()}";
throw new Exception(err);
}
stream = new FileStream(new SafeFileHandle(pipeHandle, true), FileAccess.ReadWrite);
服务器构造代码:
NamedPipeServerStream pipeServer = NamedPipeServerStreamAcl.Create("testpipe", PipeDirection.InOut, 1, PipeTransmissionMode.Message, PipeOptions.Asynchronous, 512, 512, access);
https://stackoverflow.com/questions/71338318
复制相似问题