使用电子邮件 ID 从 Active Directory 中查找用户名

Find username from Active Directory using email id(使用电子邮件 ID 从 Active Directory 中查找用户名)
本文介绍了使用电子邮件 ID 从 Active Directory 中查找用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我通过传递电子邮件 ID 从 Active Directory 中查找用户名.它工作正常.但是获取用户名需要30-40秒.还有其他更好的方法可以通过电子邮件地址从 Active Directory 中查找用户名吗?

I am finding user name from Active Directory by passing email id. It is working fine. But it takes 30-40 seconds to get the username. Is there any other better way to find the username from Active Directory by email address?

请参考我的代码:

using (PrincipalContext context = new PrincipalContext(ContextType.Domain, "domainname"))
{
    UserPrincipal userPrincipal = new UserPrincipal(context);
    PrincipalSearcher principalSearch = new PrincipalSearcher(userPrincipal);

    foreach (UserPrincipal result in principalSearch.FindAll())
    {
        if (result != null && result.EmailAddress != null && result.EmailAddress.Equals(user.Email, StringComparison.OrdinalIgnoreCase))
        {
            user.FirstName = result.GivenName;
            user.LastName = result.Surname;
        }
    }
}

推荐答案

您无需枚举所有用户即可找到一个用户!试试这个代码:

You don't need to enumerate all users to to find one of them! Try this code:

using (PrincipalContext context = new PrincipalContext(ContextType.Domain, "domainname"))
{
    UserPrincipal yourUser = UserPrincipal.FindByIdentity(context, EmailAddress);

    if (yourUser != null)
    {
        user.FirstName = yourUser.GivenName;
        user.LastName = yourUser.Surname;
    }
}

如果这不起作用,或者如果您需要一次搜索多个条件,请将 PrincipalSearcher 与 QBE(按示例查询)方法一起使用 - 搜索您需要的一个用户 - 不要在所有用户之间循环!

If that shouldn't work, or if you need to search for several criteria at once, used the PrincipalSearcher with the QBE (query-by-example) approach - search the one user you need - don't cycle through all users!

// create your domain context
using (PrincipalContext context = new PrincipalContext(ContextType.Domain, "domainname"))
{    
   // define a "query-by-example" principal - 
   UserPrincipal qbeUser = new UserPrincipal(ctx);
   qbeUser.EmailAddress = yourEmailAddress;

   // 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.....          
   }
}

这篇关于使用电子邮件 ID 从 Active Directory 中查找用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

What#39;s the difference between retrieving WindowsPrincipal from WindowsIdentity and Thread.CurrentPrincipal?(从 WindowsIdentity 和 Thread.CurrentPrincipal 检索 WindowsPrincipal 之间有什么区别?)
How do I find a user#39;s Active Directory display name in a C# web application?(如何在 C# Web 应用程序中查找用户的 Active Directory 显示名称?)
How can I authenticate against Active Directory in Nancy?(如何在 Nancy 中对 Active Directory 进行身份验证?)
How to get a username in Active Directory from a display name in C#?(如何从 C# 中的显示名称获取 Active Directory 中的用户名?)
Working with DirectoryServices in ASP.NET Core(在 ASP.NET Core 中使用 DirectoryServices)
Create Active Directory user in .NET (C#)(在 .NET (C#) 中创建 Active Directory 用户)