[英]Active Directory Search displaying properties GUID and SID returns system.Byte[] instead of Actual Values
This code produces the following output:此代码产生以下输出:
my email address,
System.Byte[], this should be SID
System.Byte[], this should be GUID
my name,
First Name,
last Name,
Middle Initial
Code:代码:
Console.WriteLine(((byte)de.Properties["objectSid"].Value.ToString());
I attempted to cast the above line.我试图投上面的线。 I get an error
我收到一个错误
Cannot convert type string to byte
无法将类型字符串转换为字节
string ObjGuid = BitConverter.ToString(de.Properties["objectguid"].Value);
I attempted the above line of code same response我尝试了上面的代码行相同的响应
((byte)de.Properties["ObjectGUID"]).Value.ToString();
Cannot convert type SystemDirectoryServices.PropertyValueCollection to byte
无法将 SystemDirectoryServices.PropertyValueCollection 类型转换为字节
byte one = Encoding.UTF8.GetString(de.Properties["ObjectGUID"]));
Cannot convert type SystemDirectoryServices.PropertyValueCollection to byte
无法将 SystemDirectoryServices.PropertyValueCollection 类型转换为字节
These are the items that I have attempted.这些是我尝试过的项目。
Console.WriteLine
requires a string. Console.WriteLine
需要一个字符串。
The problem that I see is that I am getting a list of items.我看到的问题是我得到了一个项目列表。
From this list, I am getting the underlying properties.从这个列表中,我得到了基础属性。
I am only collecting a few items of the collection of properties.我只收集属性集合中的几个项目。
I am searching inside the underlying collection and I am attempting to convert that item to a string我在底层集合中搜索,并试图将该项目转换为字符串
I think this is a basic conversion from byte to string.我认为这是从字节到字符串的基本转换。 I might have an issue with searching and manipulating an object hierarchy.
我可能在搜索和操作对象层次结构时遇到问题。
Can someone help me with this concept?有人可以帮我理解这个概念吗?
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Principal;
using System.Threading;
using System.DirectoryServices.AccountManagement;
using System.DirectoryServices;
using System.IO;
using System.Data;
using System.Management.Automation;
using System.Collections.ObjectModel;
//using System.DirectoryServices;
namespace TestRole
{
class Program
{
static void Main(string[] args)
{
//Requires Add References to using System.DirectoryServices.AccountManagement;
// and using System.DirectoryServices;
PrincipalContext ctx = new
PrincipalContext(ContextType.Domain,Environment.UserDomainName);
UserPrincipal user = new UserPrincipal(ctx);
user.EmailAddress = "MyEmail@com";
PrincipalSearcher ps = new PrincipalSearcher();
ps.QueryFilter = user;
PrincipalSearchResult<Principal> results = ps.FindAll();
Principal pc = results.ToList()[0];
DirectoryEntry de = (DirectoryEntry)pc.GetUnderlyingObject();
Console.WriteLine(de.Properties["mail"].Value.ToString());
//old code
////Console.WriteLine(de.Properties["Sid"].Value.ToString());
//Console.WriteLine(de.Properties["objectSid"].Value.ToString());
//Console.WriteLine(de.Properties["objectGUID"].Value.ToString());
//This code does the job
var sid = new
SecurityIdentifier((byte[])de.Properties["objectSid"].Value, 0);
Console.WriteLine(sid);
var guid = new Guid((Byte[])de.Properties["objectGUID"].Value);
Console.WriteLine(guid.ToString());
Console.WriteLine(de.Properties["Name"].Value.ToString());
Console.WriteLine(de.Properties["givenname"].Value.ToString());
Console.WriteLine(de.Properties["sn"].Value.ToString());
Console.WriteLine(de.Properties["initials"].Value.ToString());
Console.WriteLine(Environment.UserDomainName);
//Console.WriteLine(de.Properties["StructuralObjectClass"].Value.ToString());
}
}
}
You need to check the type of the returned values before using or converting them.在使用或转换它们之前,您需要检查返回值的类型。 The return type of
de.Properties["anyPropHere"].Value
is object
, because it will return different types, depending on the queried property. de.Properties["anyPropHere"].Value
的返回类型是object
,因为它会根据查询的属性返回不同的类型。
If you want to get the objectSid
as string you have to convert the returned bytes using the SecurityIdentifier
, as described in this post如果你想获得
objectSid
为字符串,你必须使用返回的字节转换SecurityIdentifier
,如在这个岗位
byte[] sid = (byte[])de.Properties["objectSid"].Value;
string sidStr = (new SecurityIdentifier((byte[])sid, 0)).ToString();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.