C# 如何在asp.net中使用Windows身份验证获取用户名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19676312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get user name using Windows authentication in asp.net?
提问by user2148679
I want to get user name using Windows authentication
我想使用 Windows 身份验证获取用户名
Actually I implemented "Sign in as different user",when click this button Windows security will appear there we can give credentials.
实际上我实现了“以不同用户身份登录”,当单击此按钮时,Windows 安全性将出现在那里,我们可以提供凭据。
In that time if I give some other credential it is taking current user name only. How to get that given credential user name from windows security?
在那个时候,如果我提供一些其他凭据,它只会使用当前用户名。如何从 Windows 安全中获取给定的凭据用户名?
Host application in IIS then anonymous authentication has disabled and windows authentication was enabled.
IIS 中的主机应用程序然后禁用匿名身份验证并启用 Windows 身份验证。
web.config:
网络配置:
<system.web>
<compilation debug="true" targetFramework="4.0" />
<identity impersonate="true"/>
<authorization>
<allow users="*"/>
<deny users="*"/>
</authorization>
</system.web>
<system.webServer>
<directoryBrowse enabled="true" />
<security>
<authentication>
<anonymousAuthentication enabled="false" />
<windowsAuthentication enabled="true" />
</authentication>
</security>
.cs
。CS
Here I am getting the default User name always
在这里我总是得到默认的用户名
string fullName = Request.ServerVariables["LOGON_USER"];
Any ideas? Thanks in advance
有任何想法吗?提前致谢
回答by Vishwajeet Kulkarni
I think because of the below code you are not getting new credential
我认为因为下面的代码你没有得到新的凭证
string fullName = Request.ServerVariables["LOGON_USER"];
You can try custom login page.
您可以尝试自定义登录页面。
回答by slepox
You can get the user's WindowsIdentityobject under Windows Authentication by:
您可以通过以下方式在 Windows 身份验证下获取用户的WindowsIdentity对象:
WindowsIdentity identity = HttpContext.Current.Request.LogonUserIdentity;
and then you can get the information about the user like identity.Name.
然后您可以获取有关用户的信息,例如identity.Name。
Please note you need to have HttpContext for these code.
请注意,您需要为这些代码设置 HttpContext。
回答by David Brossard
This should work:
这应该有效:
User.Identity.Name
Identity
returns an IPrincipal
Identity
返回一个 IPrincipal
Here is the link to the Microsoft documentation.
这是 Microsoft文档的链接。
回答by Kaushik Ghosh
It depends on the configuration of the application as well as on IIS as this gentleman in the below link has rightfully explained. Please see his article below
这取决于应用程序的配置以及 IIS,正如下面链接中的这位先生正确解释的那样。请看下面他的文章
http://richhewlett.com/2011/02/15/getting-a-users-username-in-asp-net/
http://richhewlett.com/2011/02/15/getting-a-users-username-in-asp-net/
回答by Ogglas
Username you get like this:
你得到的用户名是这样的:
var userName = HttpContext.Current.Request.LogonUserIdentity?.Name;
回答by Xan-Kun Clark-Davis
These are the different variables you have access to and their values, depending on the IIS configuration.
这些是您可以访问的不同变量及其值,具体取决于 IIS 配置。
Scenario 1:Anonymous Authentication in IIS with impersonation off.
场景 1:在 IIS 中进行匿名身份验证并关闭模拟。
HttpContext.Current.Request.LogonUserIdentity.Name SERVER1\IUSR_SERVER1
HttpContext.Current.Request.IsAuthenticated False
HttpContext.Current.User.Identity.Name –
System.Environment.UserName ASPNET
Security.Principal.WindowsIdentity.GetCurrent().Name SERVER1\ASPNET
Scenario 2:Windows Authentication in IIS, impersonation off.
场景 2:IIS 中的 Windows 身份验证,模拟关闭。
HttpContext.Current.Request.LogonUserIdentity.Name MYDOMAIN\USER1
HttpContext.Current.Request.IsAuthenticated True
HttpContext.Current.User.Identity.Name MYDOMAIN\USER1
System.Environment.UserName ASPNET
Security.Principal.WindowsIdentity.GetCurrent().Name SERVER1\ASPNET
Scenario 3:Anonymous Authentication in IIS, impersonation on
场景 3:IIS 中的匿名身份验证,在
HttpContext.Current.Request.LogonUserIdentity.Name SERVER1\IUSR_SERVER1
HttpContext.Current.Request.IsAuthenticated False
HttpContext.Current.User.Identity.Name –
System.Environment.UserName IUSR_SERVER1
Security.Principal.WindowsIdentity.GetCurrent().Name SERVER1\IUSR_SERVER1
Scenario 4:Windows Authentication in IIS, impersonation on
场景 4:IIS 中的 Windows 身份验证,模拟
HttpContext.Current.Request.LogonUserIdentity.Name MYDOMAIN\USER1
HttpContext.Current.Request.IsAuthenticated True
HttpContext.Current.User.Identity.Name MYDOMAIN\USER1
System.Environment.UserName USER1
Security.Principal.WindowsIdentity.GetCurrent().Name MYDOMAIN\USER1
LegendSERVER1\ASPNET
: Identity of the running process on server.SERVER1\IUSR_SERVER1
: Anonymous guest user defined in IIS.MYDOMAIN\USER1
: The user of the remote client.
图例SERVER1\ASPNET
:服务器上正在运行的进程的标识。SERVER1\IUSR_SERVER1
: IIS 中定义的匿名访客用户。MYDOMAIN\USER1
: 远程客户端的用户。
回答by Javier Flores
You can read the Name
from WindowsIdentity
:
你可以阅读Name
从WindowsIdentity
:
var user = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
return Ok(user);