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

问题描述:

我有一个使用ASP.NET窗体身份验证的ASP.NET 3.5应用程序。当在页面中编辑数据时,我希望能够获取当前登录到计算机的Windows用户名(不登录到ASP.NET应用程序,但登录到Windows)。如何从ASP.NET页面获取当前登录的Windows帐户?

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

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

而且,只有当我从Visual Studio所经营的网站工作,但部署到IIS后返回NT AUTHORITY \ SYSTEM

+1

使用Windows身份验证。否则,浏览器将如何知道,以及为什么它会向服务器发送Windows用户登录的内容? – CodeCaster 2013-04-24 06:21:36

+0

@CodeCaster,我应该采取它完全不可能使用窗体身份验证?应用程序使用角色来控制访问级别,但我希望获得当前的Windows帐户以进行一些后端试听 – StackTrace 2013-04-24 06:26:10

+0

角色没有绑定到特定的身份验证方法,您也可以在Windows身份验证中使用它们。这是一个Intranet应用程序吗? – CodeCaster 2013-04-24 07:09:47

您必须在您的配置中将认证模式设置为Windows &也禁用授权标签中的匿名用户。

+0

嗨,我有两个模块在同一个项目,即客户端和管理模块。客户端网站有登录页面和管理模块使用windowsAuth模式..如果我在.config中设置模式,那么它会影响客户端模块? – user3217843 2017-04-27 12:58:41

+0

如何禁用授权标签中的匿名用户? – 2018-02-21 12:32:02

string strName = HttpContext.Current.User.Identity.Name.ToString(); 

像你希望它做的是正确的,但你必须先设置Web服务器,参照How to Get Window NT Logged User Name Using ASP.NET(第一步骤设置Web服务器)。

尝试用下面的一行代码:

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"; 
    } 
} 

获取当前登录的用户的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帐户。

我用这个:

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

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

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

我挣扎着,挣扎着,并与此挣扎。其中一件事是我没有访问IIS的权限,因此我无法更改任何服务器设置。我不得不按照我在代码方面的能力去做。当我研究它时,许多答复都说:“像这样设置IIS”。 。 。well,当你有权访问IIS时,这很好,但我没有 - 我不得不在代码中做什么。所以,我最终处理这样的:

在我的网络配置文件,我添加下面的代码行 部分中:

<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文件中,它就在intranet上工作,它只是在本地返回错误,但只要我将上面的代码添加到本地applicationhost.config文件中,它就开始工作在本地以及。然后,我调用以下变量以返回Windows上登录用户的名称:

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

干杯!