【问题标题】:Convert SID string format to bytes array将 SID 字符串格式转换为字节数组
【发布时间】:2014-07-09 16:16:20
【问题描述】:

我想将 SID 字符串格式转换为字节数组表示,然后将其提供给 C# 中 LookupAccountSid() 方法的第二个参数。但我没有发现任何特定的内置函数可以做到这一点。例如:

SID = S-1-5-32-544

可以转化为:

(SID: 1,2,0,0,0,0,0,5,32,0,0,0,32,2,0,0)

我在一些帖子中看到了它。但如何? 有没有简单的方法来实现这一目标?基本上我想要NT Authority\NetworkService 的字节数组表示,即SID = S-1-5-20。提前感谢您的帮助。

【问题讨论】:

  • LookupAccountSid() 是您的代码还是内置函数?

标签: c# .net sid


【解决方案1】:

您应该使用来自System.Security.Principal 命名空间的SecurityIdentifier 对象:

var sid = new SecurityIdentifier("S-1-5-32-544");
byte[] bytes = new byte[sid.BinaryLength];
sid.GetBinaryForm(bytes, 0);

如果你想要它作为文本,你可以:

string strsid = string.Format("(SID: {0})", string.Join(",", bytes ));

确切地产生:

(SID:1,2,0,0,0,0,0,5,32,0,0,0,32,2,0,0)

此外,如果您想要NT Authority\NetworkService 的 SID,您可以将第一行替换为:

var sid = new SecurityIdentifier(WellKnownSidType.NetworkServiceSid, null);

【讨论】:

  • 除了初始化字节[]
【解决方案2】:

仅供参考,如果您想换一种方式(byte[] 到 SID),就是这个。就我而言,byte[] 来自 ManagementEventWatcher:

ManagementBaseObject ne = e.NewEvent;
var securityIdentifier = new System.Security.Principal.SecurityIdentifier((byte[])ne.Properties["SID"].Value, 0);

您可以使用securityIdentifier.ToString() 将 SID 作为字符串获取。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-11
    • 1970-01-01
    • 2011-09-17
    • 2018-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多