从本地的WindowsIdentity获取用户名和电子邮件

问题描述 投票:0回答:1

我正在尝试构建一个Web应用程序以部署在我们的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个回答
0
投票

1)我缺少一个可以使用NuGet获得的名为System.DirectoryServices的程序包/参考。

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();
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.