6

How can I get the user name of Windows Login? I used these methods:

  • Environment.UserName
  • WindowsIdentity.GetCurrent().Name;

But, when I PUT this inside IIS server, it takes the name of the server, not my machine.

live-love
  • 48,840
  • 22
  • 240
  • 204
soamazing
  • 1,656
  • 9
  • 25
  • 32

3 Answers3

5

Try Page.User.Identity.Name. This should be what you're looking for. This property is derived from HttpContext and represents the logged in user security information for the current HTTP request.

If the result is null, then I would suspect that the IIS settings is not configured properly. Try the advice in the following links:

http://forums.asp.net/t/1689878.aspx/1
HttpContext.Current.User.Identity.Name is Empty

Community
  • 1
  • 1
code4life
  • 15,655
  • 7
  • 50
  • 82
  • Then no user is logged in, or you don't have any authorization methods for the specific pages. – Erik Philips May 15 '12 at 20:47
  • @ErikPhilips I'm inside the lan, when I use VS localhost working but the app inside IIS not work. – soamazing May 15 '12 at 21:07
  • @soamazing That is because Casini works different that IIS. Again, either your application or IIS is not configured to automatically log people in using the domain, or some other type of authentication. – Erik Philips May 16 '12 at 06:36
0

Use this System.Environment.GetEnvironmentVariable("UserName")

0

Take a look at:

https://richhewlett.com/2011/02/15/getting-a-users-username-in-asp-net/

Here are the main examples from the link above:

Scenario 1: Anonymous Authentication in IIS with impersonation off.

HttpContext.Current.Request.LogonUserIdentity.Name  -> COMPUTER1\IUSR_COMPUTER1
HttpContext.Current.Request.IsAuthenticated -> False
HttpContext.Current.User.Identity.Name  -> –
System.Environment.UserName -> ASPNET
Security.Principal.WindowsIdentity.GetCurrent().Name    -> COMPUTER1\ASPNET

Scenario 2: Windows Authentication in IIS, impersonation off.

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    -> COMPUTER1\ASPNET

Scenario 3: Anonymous Authentication in IIS, impersonation on

HttpContext.Current.Request.LogonUserIdentity.Name  -> COMPUTER1\IUSR_COMPUTER1
HttpContext.Current.Request.IsAuthenticated -> False
HttpContext.Current.User.Identity.Name  -> –
System.Environment.UserName -> IUSR_COMPUTER1
Security.Principal.WindowsIdentity.GetCurrent().Name    -> COMPUTER1\IUSR_COMPUTER1

Scenario 4: Windows Authentication in IIS, impersonation on

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

Note:

SERVER1\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.
live-love
  • 48,840
  • 22
  • 240
  • 204