.NET中安全身份的本地化

11

我希望实现在.NET中用于服务/客户端通信的命名管道,并发现这段代码。在设置管道的服务器端时,需要为其设置安全描述符。他们是这样做的:

PipeSecurity pipeSecurity = new PipeSecurity();

// Allow Everyone read and write access to the pipe.
pipeSecurity.SetAccessRule(new PipeAccessRule("Authenticated Users",
    PipeAccessRights.ReadWrite, AccessControlType.Allow));

// Allow the Administrators group full access to the pipe.
pipeSecurity.SetAccessRule(new PipeAccessRule("Administrators",
    PipeAccessRights.FullControl, AccessControlType.Allow));

但我现在正在看它,并且我担心使用SID字符串,或者使用Authenticated UsersAdministrators这样的术语。无论是在中文还是其他语言中,这些名称是否有保证能够被准确呼叫?


好的。我已经确认过了。看起来它能够工作。 - ahmd0
5
你应该考虑把这个内容拆成一个合适的问题/答案对。 - slugster
是的,您可以回答自己的问题。 - DWright
答案提取。根据OP的帖子或要求删除。 - Shimmy Weitzhandler
2个回答

5

您可以使用WellKnownSidType枚举获取sid并将其翻译为IdentityReference:

        var sid = new SecurityIdentifier(WellKnownSidType.AuthenticatedUserSid, null);
        var everyone = sid.Translate(typeof(NTAccount));
        security.AddAccessRule(new PipeAccessRule(everyone, PipeAccessRights.ReadWrite, AccessControlType.Allow));

1
“Everyone” 不是一个好主意。你可能至少需要 WellKnownSidType.AuthenticatedUserSid。除了担心它可能不起作用之外,你使用 WorldSid(Everyone)的特定原因是什么? - doug65536
没错!已更改。 - Andrey Burykin
1
当你遇到未知的安全问题时,使用Everyone是一个好主意,它可以让你通过一项健全性测试来确保你的代码正常运行,之后你可以缩小需要的安全级别。 - TheLegendaryCopyCoder

2
我想到了这个替代方案:
PipeSecurity pipeSecurity = new PipeSecurity();

// Allow Everyone read and write access to the pipe.
pipeSecurity.SetAccessRule(new PipeAccessRule(
    "Authenticated Users",
    new SecurityIdentifier(WellKnownSidType.AuthenticatedUserSid, null),   
    PipeAccessRights.ReadWrite, AccessControlType.Allow));

// Allow the Administrators group full access to the pipe.
pipeSecurity.SetAccessRule(new PipeAccessRule(
    "Administrators",
    new SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid, null),   
    PipeAccessRights.FullControl, AccessControlType.Allow));

1
对我来说这不起作用,没有重载函数可以同时接受字符串和SecurityIdentifier请参见此处。请删除字符串字面量“Authenticated Users”和“Administrators”。 - doug65536

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接