如何在.net中将SID转换为String

Maj*_*ell 22 .net c#

我想将SID的System.Byte []类型转换为String.

我的代码:

string path = "LDAP://DC=abc,DC=contoso,DC=com";
DirectoryEntry entry = new DirectoryEntry(path);
DirectorySearcher mySearcher = new DirectorySearcher(entry);

mySearcher.Filter = "(&(objectClass=user)(samaccountname=user1))";
results = mySearcher.FindAll();
foreach (SearchResult searchResult in results)
{
    Console.WriteLine(searchResult.Properties["ObjectSID"][0].ToString());
}
Run Code Online (Sandbox Code Playgroud)

我试过这个,但它从我当前登录的域中获取值,并且我需要来自给定的域.

System.Security.Principal.NTAccount(user1)
    .Translate([System.Security.Principal.SecurityIdentifier]).value
Run Code Online (Sandbox Code Playgroud)

M A*_*ifi 49

看一下SecurityIdentifier类.然后你可以做一些简单的事情,比如

var sidInBytes = (byte[]) *somestuff*
var sid = new SecurityIdentifier(sidInBytes, 0);
// This gives you what you want
sid.ToString();
Run Code Online (Sandbox Code Playgroud)