简体   繁体   中英

How to get windows username

I am trying to get the windows username on an aspx page.

I have tried the following:

<% System.Threading.Thread.CurrentPrincipal = new System.Security.Principal.WindowsPrincipal(System.Security.Principal.WindowsIdentity.GetCurrent()) %>
<%= System.Threading.Thread.CurrentPrincipal.Identity.Name %>

But all that does is give me IIS APPPOOL/SBalance - which is not the windows username!

However if I add the following server control:

<asp:LoginName ID="LoginName1" runat="server" />

this does successfully pre-populate with my domain and username.

So how can I get the domain/username as a string on the aspx page without using a login control?

Thanks

如果您使用 Windows 身份验证,您可以通过User.Identity.Name获取用户名

string windowsLogin = Page.User.Identity.Name;

you will need to enable impersonation in the web.config

<identity impersonate="true" />

then you need to enable windows impersonation in IIS and disable anonymous authentication

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM