【问题标题】:Get Users Name and Email from WindowsIdentity on local从本地的 WindowsIdentity 获取用户名和电子邮件
【发布时间】:2020-01-30 01:08:33
【问题描述】:

我正在尝试构建一个 webapp 以部署在我们的 Intranet 上。我正处于我想要用户信息的阶段。

据我所知,我已经通过实施正确设置了身份验证:

在 Startup->ConfigureServices 中:

services.AddRazorPages();
services.AddAuthentication(IISDefaults.AuthenticationScheme);

并在启动->配置中:

app.UseAuthentication();
app.UseAuthorization();

现在在一个页面模型中,我一直在尝试获取用户信息,但似乎无法通过 userName 和 Groups。

    public void OnGet()
    {

        var wi = (WindowsIdentity)User.Identity;

        this.email = wi.Claims.FirstOrDefault(
            c => c.Type == ClaimTypes.Email)?.Value;
        this.claimsCount = wi.Claims.Count().ToString();
        this.claims = wi.Claims.Select(x => x.Type.ToString()).ToList();
        this.authenticationType= wi.AuthenticationType.ToString();
        this.name = wi.Name;

        if (wi.Groups != null)
            foreach (var group in wi.Groups)
            {

                try
                {
                    this.groups.Add(group.Translate(typeof(NTAccount)).ToString());
                }
                catch (Exception)
                {
                    // ignored
                }
            }

    }

这就是我在模型中得到的结果。

this.email = null
this.claimsCount = 51
this.claims = {A list composed of claims/name, claims/primarysid, claims/primarygroupsid, claims/groupsid x 48}
this.authenticationType = Negotiate 
this.name = my login username

如何从这里获取用户的电子邮件地址和名字(即:“John Doe”而不是“CA/jdoe”)?我的研究使我相信我可能没有权限在我的本地计算机上获取此信息。

如果是这种情况,我该如何去检查部署服务器以查看它是否具有获取此信息所需的权限?

【问题讨论】:

    标签: asp.net windows-authentication intranet


    【解决方案1】:

    1) 我缺少一个名为 System.DirectoryServices 的包/引用,我可以使用 NuGet 获得它。

    2) 我添加了以下代码行。

            var wi = (WindowsIdentity)User.Identity;
    
            System.DirectoryServices.DirectoryEntry user = new System.DirectoryServices.DirectoryEntry($"LDAP://<SID={wi.User.Value}>");
    
            user.RefreshCache(new[] { "givenName", "sn" });
    
            this.name = user.Properties["givenName"].Value.ToString();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-16
      • 2015-02-14
      • 2023-01-25
      • 2012-04-28
      • 2015-01-23
      • 1970-01-01
      相关资源
      最近更新 更多