我想将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());
}
我试过这个,但它从我当前登录的域中获取值,并且我需要来自给定的域。
System.Security.Principal.NTAccount(user1)
.Translate([System.Security.Principal.SecurityIdentifier]).value
答案 0 :(得分:47)
看一下SecurityIdentifier课程。然后你可以做一些简单的事情,比如
var sidInBytes = (byte[]) *somestuff*
var sid = new SecurityIdentifier(sidInBytes, 0);
// This gives you what you want
sid.ToString();
答案 1 :(得分:1)
这就是我所做的,经过一些阅读后,将值存储在oct中似乎更安全。 如果你不知道另一边是哪些服务器。 下面的代码显示了如何获得所需的结果
private static string ExtractSinglePropertyValueFromByteArray(object value)
{
//all if checks etc has been omitted
string propertyValue = string.Empty;
var bytes = (byte[])value;
var propertyValueOct = BuildOctString(bytes); // 010500....etc
var propertyValueSec = BuildSecString(bytes); // S-1-5-...etc
propertyValue = propertyValueSec;
return propertyValue;
}
private static string BuildSecString(byte[] bytes)
{
return new SecurityIdentifier(bytes,0).Value.ToString();
}
private static string BuildOctString(byte[] bytes)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < bytes.Length; i++)
{
sb.Append(bytes[i].ToString("X2"));
}
return sb.ToString();
}
答案 2 :(得分:0)
在directoryEntry ....
中加载属性后var usrId = (byte[])directoryEntry.Properties["objectSid"][0];
var objectID = (new SecurityIdentifier(usrId,0)).ToString();