如何从 ASP.NET 页面获取当前登录的 Windows 帐户?

Posted

技术标签:

【中文标题】如何从 ASP.NET 页面获取当前登录的 Windows 帐户?【英文标题】:How do I get the currently loggedin Windows account from an ASP.NET page? 【发布时间】:2013-04-17 13:45:01 【问题描述】:

我有一个使用 ASP.NET 表单身份验证的 ASP.NET 3.5 应用程序。当在页面中编辑数据时,我希望能够获取当前登录到计算机(未登录到 ASP.NET 应用程序,而是登录到 Windows)的 Windows 用户名。

如果我使用Context.User.Identity.Name.Tostring(),我会得到登录到 ASP.NET 应用程序的用户名,但我需要 Windows 帐户名。

System.Security.Principal.WindowsIdentity.GetCurrent().Name.Tostring()

此外,它仅在我从 Visual Studio 运行网站时才有效,但在部署到 IIS 后它返回 NT AUTHORITY\SYSTEM

【问题讨论】:

使用 Windows 身份验证。否则浏览器怎么会知道,为什么它会向服务器发送什么 Windows 用户登录? @CodeCaster,我应该认为它完全不可能使用表单身份验证吗?应用程序使用角色来控制访问级别,但我想获取当前 Windows 帐户以进行某些后端试听 角色不绑定到特定的身份验证方法,您也可以将它们与 Windows 身份验证一起使用。这是一个内网应用吗? 【参考方案1】:

您必须在配置中将身份验证模式设置为Windows,并在授权标签中禁用匿名用户。

【讨论】:

嗨,我在同一个项目中有两个模块,即客户端和管理模块。客户端网站有登录页面,管理模块使用 windowsAuth 模式..如果我在 .config 中设置模式,那么它会影响客户端模块? 如何在授权标签中禁用匿名用户?【参考方案2】:

要让当前登录的用户使用 Windows 帐户,您必须使用 Windows authentication 而不是 Forms authentication

System.Security.Principal.WindowsIdentity.GetCurrent().Name.Tostring() 也仅在我从 Visual Studio 运行网站但之后才有效 部署到 IIS 它返回 NT AUTHORITY\SYSTEM

它显示应用程序当前用户。当您在 Visual Studio Web 服务器上托管应用程序时,它使用您的本地帐户。但是,当您使用不同的凭据登录 Web 应用程序时,它将始终显示您当前的 Windows 登录信息。

在您的情况下,部署到 IIS 的应用程序使用 NT AUTHORITY\SYSTEM 帐户。

【讨论】:

【参考方案3】:

要在 C# 中获取当前登录的用户到 Windows,请使用:

string Username = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();

【讨论】:

【参考方案4】:

我为此挣扎,挣扎,挣扎。其中一件事是我无法访问已锁定的 IIS,因此我无法更改任何服务器设置。我不得不去做我能够在代码中做的事情。当我研究它时,许多回复说,“这样设置IIS”。 . .嗯,当您可以访问 IIS 时,这很好,但我没有——我必须使用我可以在代码中做的事情。所以,我最终这样处理它:

在我的 web 配置文件中,我在 部分:

<system.webServer>
<security>
  <authentication>
    <anonymousAuthentication enabled="false" />
    <windowsAuthentication enabled="true" />
  </authentication>
</security>
</system.webServer>

然后,它在我的本地返回了一个错误,我必须进去修复它。我去了位于我机器上以下路径中的 applicationhost.config 文件(你的可能不同):

C:\users\"你的用户名"\My Documents\"yourIISInstallation"\config\applicationhost.config

我将以下设置更改为“允许”,该设置已设置为“拒绝”:

<section name="anonymousAuthentication" overrideModeDefault="Deny" />

改为

<section name="anonymousAuthentication" overrideModeDefault="Allow" />

<section name="windowsAuthentication" overrideModeDefault="Deny" />

<section name="windowsAuthentication" overrideModeDefault="Allow" />

<sectionGroup name="authentication">

部分。在我发现这个修复之前,我把头发拉出来了。我希望这可以帮助别人。一旦我将上面的代码放入 webconfig 文件中,它就可以在内网上运行,它只是在我的本地返回错误,但是一旦我将上面的代码添加到我的本地 applicationhost.config 文件中,它就开始在我的本地运行以及。然后,我调用了以下变量来返回 windows 上登录用户的名称:

    HttpContext.Current.User.Identity.Name.ToString().Substring((HttpContext.Current.User.Identity.Name.ToString().IndexOf("\\")) + 1);

干杯!

【讨论】:

【参考方案5】:

我用这个:

System.Security.Principal.WindowsPrincipal user;
user = new WindowsPrincipal(this.Request.LogonUserIdentity);
this.Request.LogonUserIdentity.Impersonate();
user_name = user_name.Substring(user_name.LastIndexOf("\\") + 1);

【讨论】:

在IIS中部署时,这是否仍然在计算机中获取当前登录Windows身份验证?并且不需要 authentication=windows 在 web config 中?【参考方案6】:
string strName = HttpContext.Current.User.Identity.Name.ToString();

如您所愿是正确的,但您需要先设置网络服务器,参考 How to Get Window NT Logged User Name Using ASP.NET(设置网络服务器的第一步)。

【讨论】:

【参考方案7】:

试试下面这行代码:

string loggedOnUser = string.Empty;
 loggedOnUser = Request.ServerVariables.Get("AUTH_USER");

当您从 Visual Studio 运行应用程序时,您可能无法获取值...在 IIS 中部署后检查它。

要获取用户名,请使用:

string userName = string.Empty;
using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, "Your Domain Name"))

    UserPrincipal user = new UserPrincipal(pc);
    user = UserPrincipal.FindByIdentity(pc, "User ID Will Come here");
    if (user != null)
    
        userName = user.GivenName + " " + user.Surname;

    
    else
    
        //return string.Empty;
        userName = "User Not Found";
    

【讨论】:

【参考方案8】:

我设法按照以下链接中方法 1 中的说明解决了这个问题 - https://support.microsoft.com/en-us/help/896861/you-receive-error-401-1-when-you-browse-a-web-site-that-uses-integrate 简而言之,禁用除 Windows 身份验证之外的所有身份验证方法。 在管理员帐户下打开 regedit,找到 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa\MSV1_0,右键单击节点并选择新建,然后选择多字符串值。 输入“BackConnectionHostNames”并单击 Enter。 对于值,输入您尝试设置访问权限的网站,然后单击确定。 重新启动 IIS 完成后,我可以使用 HttpContext.Current.User.Identity.Name 获取当前的 Windows 用户,WindowsPrincipal(this.Request.LogonUserIdentity) 也让我获得了登录的 Windows 用户名。 作为参考 System.Environment.UserName 和 System.Security.Principal.WindowsIdentity.GetCurrent().Name,这两个仍然给 IIS 用户。

这花了我很长时间才弄清楚。祝你好运。 IIS 是一场醒着的噩梦!

【讨论】:

以上是关于如何从 ASP.NET 页面获取当前登录的 Windows 帐户?的主要内容,如果未能解决你的问题,请参考以下文章

使用 ASP.NET 表单身份验证时如何获取当前登录用户的用户 ID?

asp.net 如何获得 当前用户 userid

asp.net如何让未登录用户登录后能自动跳转到登录前访问的页面?

如何在 ASP.NET MVC 中获取当前用户

如何在 ASP NET CORE 3.0 中获取当前登录的用户 ID?

如何在 ASP.NET 中获取登录用户信息