簡體   English   中英

按名字和姓氏搜索用戶登錄ID

[英]Search a user login id by first name and last name

我發現如何從活動目錄中獲取用戶列表?

當我只有幾個用戶時,這很有幫助,但我在AD中有這么多用戶,所以當我運行我的查詢時

if ((String)(entry.Properties["sn"].Value) == "lname"
     && (String)(entry.Properties["givenName"].Value) == "fname")
{
    return entry.Properties["samAccountName"].Value.ToString();
}

完成花了太長時間。

如何通過名字和姓氏搜索某個特定用戶登錄ID?

由於您使用的是.NET 4,因此應該查看System.DirectoryServices.AccountManagement (S.DS.AM)命名空間。 在這里閱讀所有相關內容:

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

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

// find a user - by e.g. his "samAccountName", or the Windows user name or something
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "SomeUserName");

if(user != null)
{
   // do something here....     
   string samAccountName = user.SamAccountName;
}

如果找不到用戶名指定的用戶,還可以使用新的搜索功能:

// define a "query-by-example" principal - here, we search for a UserPrincipal 
// and with the first name (GivenName) and a last name (Surname) 
UserPrincipal qbeUser = new UserPrincipal(ctx);
qbeUser.GivenName = firstName;
qbeUser.Surname = lastName;

// create your principal searcher passing in the QBE principal    
PrincipalSearcher srch = new PrincipalSearcher(qbeUser);

// find all matches
foreach(var found in srch.FindAll())
{
    // do whatever here - "found" is of type "Principal" - it could be user, group, computer.....          
}

新的S.DS.AM使得在AD中與用戶和群組玩游戲變得非常容易! 而找到一個用戶也應該相對快速。

您應該使用AD服務器進行過濾。 通過提供LDAP語法過濾器來完成此操作。 另外,使用FindAllpropertiesToLoad參數僅指定所需的propertiesToLoad

    public static SearchResultCollection FindByName(
        string domain, string firstName, string lastName, string[] properties) {
        var rootEntry = new DirectoryEntry("LDAP://" + domain);
        var filter = string.Format("(&(sn={0})(givenName={1}))", lastName, firstName);
        var searcher = new DirectorySearcher(rootEntry, filter, properties);
        return searcher.FindAll();
    }

    // Using the method:
    var result = FindByName("mydomain", "Robert", "Smith", new[] { "samAccountName" })[0];
    string uName = (string)result.Properties["samAccountName"][0];

您需要在searcher上設置QueryFilter屬性,然后調用searcher.FindOne()而不是searcher.FindAll() 可以將查詢過濾器設置為UserPrincipal對象,您可以在其中設置要搜索的字段。

Microsoft在Query By Example頁面中有一個很好的例子,盡管他們的例子希望找到幾個符合給定條件的對象。

對您的要求進行特別字面改編的相關問題將是

using (var context = new PrincipalContext(ContextType.Domain, "mydomain.com"))
{
    using (var searcher = new PrincipalSearcher(new UserPrincipal(context) { GivenName = "fname", Surname = "lname" }))
    {
            foreach (var result in searcher.FindAll())
            {
                DirectoryEntry de = result.GetUnderlyingObject() as DirectoryEntry;
                return de.Properties["samAccountName"].Value.ToString();
            }
    }
}

如果entryIEnumerable集合的一部分,您可以執行以下操作:

var entries = {do your population of the collection here}

var entry = entries.Where(e=>e.Properties["sn"].Value.ToString() == "lname"
    && e=>.Properties["givenName"].Value.ToString() == "fname")
    .FirstOrDefault();

暫無
暫無

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

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