简体   繁体   English

我可以从 email 地址而不是 ASP.NET Core 和 DirectoryServices 中的用户名获取用户身份吗?

[英]Can I get a users identity from an email address instead of a username in ASP.NET Core and DirectoryServices?

I have a need for users to be able to enter an email address and after clicking submit, I'd like to get some Active Directory information from the email address entered.我需要用户能够输入 email 地址,单击提交后,我想从输入的 email 地址获取一些 Active Directory 信息。 Our usernames unfortunately don't follow a single naming convention so I'm unable to reliably guess the username from the email address entered.不幸的是,我们的用户名不遵循单一的命名约定,因此我无法从输入的 email 地址中可靠地猜出用户名。

I'm fine with looking up information from a username, is there a way of back tracking to an identity from an email address entered using DirectoryServices or something similar?我可以从用户名中查找信息,有没有办法从使用 DirectoryServices 或类似的东西输入的 email 地址回溯到身份?

Thanks,谢谢,

Edit and some clarifiction:编辑和一些澄清:

On the form, it's not the authenticated users email address that I would be looking up, it's the email address that was entered in the form.在表单上,我要查找的不是经过身份验证的用户 email 地址,而是在表单中输入的 email 地址。

    public class ManagerDetails
    {
        public string GetManagersName(string emailAddress)
        {
            PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
            UserPrincipal mgr = UserPrincipal.FindByIdentity(ctx, emailAddress);

            string managersName = mgr.GivenName;

            return managersName;
        }
    }

This piece of code works, but only if the username matches the email address.这段代码有效,但前提是用户名与 email 地址匹配。

For example, this works, Joe.Bloggs@mydomain.com (email) and Joe.Bloggs (domain username).例如,这有效,Joe.Bloggs@mydomain.com(电子邮件)和 Joe.Bloggs(域用户名)。

If it's Joe.Bloggs@mydomain.com (email) and BloggsJ (domain username) it fails.如果是 Joe.Bloggs@mydomain.com(电子邮件)和 BloggsJ(域用户名),则失败。

This is obviously because it's using the email address as the full username but hopefully it explains what I'm trying to achieve.这显然是因为它使用 email 地址作为完整的用户名,但希望它能解释我想要实现的目标。

I'm still refining but this works, it allows me to use directory searcher to get a name from an email address entered.我仍在完善,但这有效,它允许我使用目录搜索器从输入的 email 地址中获取名称。

        public string GetManagersName(string emailAddress)
        {
            string userName = GetManagersUserNameByEmailAddress(emailAddress);

            PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
            UserPrincipal usr = UserPrincipal.FindByIdentity(ctx, userName);

            string managersName = usr.GivenName;

            return managersName;
        }

        private string GetManagersUserNameByEmailAddress(string emailAddress)
        {
            DirectorySearcher adSearcher = new DirectorySearcher();
            adSearcher.Filter = ("mail=" + emailAddress);
            adSearcher.PropertiesToLoad.Add("samaccountname");

            SearchResult result = adSearcher.FindOne();

            DirectoryEntry user = result.GetDirectoryEntry();

            string userName = user.Properties["samaccountname"].Value.ToString();

            return userName;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在ASP.Net Web窗体标识中使用“用户名”而不是“电子邮件地址”登录 - How to login with “username” instead of “email address” in ASP.Net Web Forms Identity 如何将ASP.NET MVC默认登录页面更改为通过用户名而不是电子邮件地址登录? - How do I change the ASP.NET MVC default login page to login by username instead of email address? 我如何获得UserName,我使用ASP.NET核心MVC和身份? - How I get the UserName, I use ASP.NET Core MVC and Identity? 如何在 ASP.Net Core 5 Identity 中从 Google OAUTH 获取刷新令牌? - How can I get a refresh-token from Google OAUTH in ASP.Net Core 5 Identity? 在ASP.NET Core中从EF读取身份用户信息 - Reading Identity Users Information From EF in ASP.NET Core asp.net 核心身份来自 BaseApplicationUser 的多个用户 - asp.net core identity Multiple users from BaseApplicationUser 在ASP.NET Core身份中编辑用户 - Editing users in ASP.NET Core Identity 更改Asp.NET身份验证以注册电子邮件地址,但不注册用户名 - Changing Asp.NET Identity Authentication to register email address, but never username 我如何使用 ASP.NET Core 3.1 身份验证和注册来自业务逻辑层的用户 - How do i Authenticate and register users from the business Logic Layer with ASP.NET Core 3.1 Identity 如何在ASP.NET Core Identity中使用角色GUID代替名称? - How can I use role GUIDs instead of names in ASP.NET Core Identity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM