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

24

我有一个使用ASP.NET Forms身份验证的ASP.NET 3.5应用程序。当在页面上编辑数据时,我想要获取当前登录到计算机(而不是登录到ASP.NET应用程序)的Windows用户名。

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

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

另外,只有在我从Visual Studio运行网站时它才起作用,但是在部署到IIS后,它返回 NT AUTHORITY\SYSTEM


3
请使用Windows身份验证。否则,浏览器如何知道并向服务器发送哪个Windows用户已登录?请注意让翻译内容通俗易懂,但不要改变原意。 - CodeCaster
@CodeCaster,我应该认为使用表单身份验证是完全不可能的吗?该应用程序使用角色来控制访问级别,但我想获取当前Windows帐户以进行一些后端审计。 - StackTrace
角色不绑定特定的身份验证方法,您也可以将其与Windows身份验证一起使用。这是一个内部网络应用程序吗? - CodeCaster
8个回答

18

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


1
嗨,我在同一项目中有两个模块,即客户端和管理员模块。客户网站有登录页面,管理员模块使用Windows身份验证模式。如果我在.config中设置模式,那么它会影响客户端模块吗? - user3217843
2
如何在授权标记中禁用匿名用户? - Ciaran Gallagher

11
为获取当前登录的Windows账户用户,你需要使用 Windows身份验证 而不是 Forms身份验证

System.Security.Principal.WindowsIdentity.GetCurrent().Name.Tostring() 只适用于我从 Visual Studio 运行网站时,但在部署到 IIS 后会返回 NT AUTHORITY\SYSTEM。

该代码显示应用程序的当前用户。当你将应用程序托管在Visual Studio Web服务器上时,它使用你的本地帐户。然而,当你使用不同的凭据登录Web应用程序时,它始终显示你当前Windows登录的用户。
在你的情况下,部署到IIS的应用程序使用 NT AUTHORITY\SYSTEM 帐户。

9

使用以下代码在C#中获取当前已登录的Windows用户:

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

8
我一直在苦苦挣扎,因为我无法访问被锁定的IIS,所以我不能更改任何服务器设置。我只能按照我的代码能力去做。当我进行研究时,许多回复都说:“像这样设置IIS”……好吧,当你可以访问IIS时,这很棒,但我不能这样做 - 我必须使用代码来解决问题。所以,我最终是这样处理的:
在我的Web配置文件中,在
部分中添加了以下代码行:
<system.webServer>
<security>
  <authentication>
    <anonymousAuthentication enabled="false" />
    <windowsAuthentication enabled="true" />
  </authentication>
</security>
</system.webServer>

接着,在我的本地出现了错误,我必须去解决它。我前往位于以下路径的applicationhost.config文件进行修改(您的路径可能不同):

C:\users\"您的用户名"\My Documents\"您的IIS安装文件夹"\config\applicationhost.config

然后,我将下面的设置从"拒绝"更改为"允许":

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

更改为

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

并且

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

为了

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

in the

<sectionGroup name="authentication">

在我找到这个解决方法之前,我因此问题苦恼了很久。希望这能帮助到别人。当我将上述代码放入webconfig文件中时,它在内部网络上运行良好,但在本地却返回错误。但是,当我将上述代码添加到我的本地applicationhost.config文件中后,它也开始在本地正常工作了。然后,我调用以下变量来返回Windows登录用户的名称:

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

干杯!


4
我使用这个:
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中部署时,它是否仍然获取计算机中当前的登录窗口身份验证?而且在web config中不需要authentication=windows吗? - Rigel1121

1

0
尝试使用以下一行代码:
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";
    }
}

0

我通过按照以下链接中的方法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。对于Value,输入您要设置访问权限的网站,然后点击OK。

重新启动IIS。完成后,我能够使用HttpContext.Current.User.Identity.Name获取当前的Windows用户,WindowsPrincipal(this.Request.LogonUserIdentity)也可以获取已登录的Windows用户名。

供参考,System.Environment.UserName和System.Security.Principal.WindowsIdentity.GetCurrent().Name,这两个仍然给出了IIS用户。

这花费了我很长时间才解决。祝你好运。IIS是一个噩梦!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接