使用 Azure AD 进行身份验证时,从 Razor.Pages 项目中的 User.Identity 获取完整用户名

Getting the full user name from User.Identity in Razor.Pages project when authenticating using Azure AD

我正在 .Net Core 3.1 中开发我的 Razor.Pages Web 应用程序,我使用我公司的 AD 配置了身份验证。我可以毫无问题地使用 User.Identity.Name 来获取 user@domain 值,但我需要获取登录人员的全名,以便我可以将查询的某些结果过滤为 SQL 基于用户全名的数据库。

我试着用谷歌搜索,但没有找到解决我问题的方法。谢谢!

经过一番深入研究后,我终于设法创建了一种方法,它接收登录用户的 User.Identity.Name 和全名 returns。 下面是该方法的一个片段!

    public static string GetFullName(string domainName)
    {
        string fullName = "";
        UserPrincipal principal;

        using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
        {
            principal = UserPrincipal.FindByIdentity(ctx, domainName);
        }

        if (principal != null)
            fullName = $"{principal.GivenName} {principal.Surname}";
        else
            fullName = domainName;

        return fullName;
    }