簡體   English   中英

如何使用.net中的nativeguid在Active Directory中找到用戶?

[英]How do I find a user in Active Directory using their nativeguid in .net?

我遇到一個問題,其中運行DirectorySearcher並不總是成功返回結果。 例如,我將使用以下方法發送經理的GUID以轉換字符串(NativeGuid):

public static string Guid2OctetString(string objectGuid)
{
    System.Guid guid = new Guid(objectGuid);
    byte[] byteGuid = guid.ToByteArray();
    string queryGuid = "";
    foreach (byte b in byteGuid)
    {
        queryGuid += @"\" + b.ToString("x2");
    }
    return queryGuid;
}

據推測,這會將guid字符串轉換為Active Directory可用的東西。 當我通過它運行經理的NativeGuid ,我得到了結果。 但是,在應用程序的下一步中,我以相同的方式運行了一個directReports的Guid,但沒有結果。 用戶確實存在,如果我拉出DirectoryEntry ,則可以獲取該條目,但是我不希望完整的條目,因為它處理起來太慢。 我需要能夠使用DirectorySearcher來縮小字段的速度。 為什么我在某些用戶而不是其他用戶的目錄搜索中得到鵝蛋?

如果您使用的是.NET 3.5及更高版本,則應簽出System.DirectoryServices.AccountManagement (S.DS.AM)命名空間。 在這里閱讀所有內容:

基本上,您可以定義域上下文並輕松找到AD中的用戶和/或組:

// set up domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

// find a user
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, IdentityType.Guid, YourGuid.ToString());

if(user != null)
{
   // do something here....     
}

新的S.DS.AM使得與AD中的用戶和組玩起來非常容易!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM