将SID字符串格式转换为字节数组 [英] Convert SID string format to bytes array
问题描述
我想将SID字符串格式转换为字节数组表示形式,然后将其提供给C#中LookupAccountSid()
方法的第二个参数.但是我没有发现可以执行此操作的任何内置函数.例如:
I want to convert SID string format to bytes array representation which in turn will be supplied to LookupAccountSid()
method's second argument in C#. But I don't find any particular in built function which can do this. For example:
SID = S-1-5-32-544
SID = S-1-5-32-544
可以转换为:
(SID:1,2,0,0,0,0,0,5,32,0,0,0,32,2,0,0)
(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
.预先感谢您的帮助.
I saw it in some posts. but how?
Is there a easy way to achieve this? Basically I want the byte array representation of the NT Authority\NetworkService
which is SID = S-1-5-20
. Thanks in advance for your help.
推荐答案
您应使用 SecurityIdentifier
对象:
You should use SecurityIdentifier
object from System.Security.Principal
namespace:
var sid = new SecurityIdentifier("S-1-5-32-544");
byte[] bytes = new byte[sid.BinaryLength];
sid.GetBinaryForm(bytes, 0);
,如果您希望将其作为文本,则可以:
and if you want it as a text, you can then:
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)
(SID: 1,2,0,0,0,0,0,5,32,0,0,0,32,2,0,0)
此外,如果您希望SID为NT Authority\NetworkService
,则可以将第一行替换为:
Moreover, if you want SID of NT Authority\NetworkService
, you can replace first line with:
var sid = new SecurityIdentifier(WellKnownSidType.NetworkServiceSid, null);
这篇关于将SID字符串格式转换为字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!