繁体   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