36

I want to get user name using Windows authentication

Actually, I implemented "Sign in as different user", when click this button Windows security will appear there we can give credentials.

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?

Host application in IIS then anonymous authentication has disabled and windows authentication was enabled.

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>

</system.webserver>

.cs

Here I am getting the default User name always

string fullName = Request.ServerVariables["LOGON_USER"];

Any ideas?

starball
  • 20,030
  • 7
  • 43
  • 238
user2148679
  • 459
  • 1
  • 6
  • 18

8 Answers8

46

These are the different variables you have access to and their values, depending on the IIS configuration.

Scenario 1: Anonymous Authentication in IIS with impersonation off.

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.

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

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

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

Legend
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.

Source

Xan-Kun Clark-Davis
  • 2,664
  • 2
  • 27
  • 38
38

You can get the user's WindowsIdentity object under Windows Authentication by:

WindowsIdentity identity = HttpContext.Current.Request.LogonUserIdentity;

and then you can get the information about the user like identity.Name.

Please note you need to have HttpContext for these code.

slepox
  • 792
  • 4
  • 9
  • Thanks for your response...I tried this but not working..if I give any wrong password in windows security it is taking how to solve this? – user2148679 Oct 30 '13 at 08:54
  • 2
    You mean wrong password input at the Windows Auth dialog? It should not allow you pass authentication. – slepox Nov 01 '13 at 03:37
7

This should work:

User.Identity.Name

Identity returns an IPrincipal

Here is the link to the Microsoft documentation.

David Brossard
  • 13,584
  • 6
  • 55
  • 88
4

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

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

Kaushik Ghosh
  • 11,912
  • 1
  • 15
  • 9
  • 1
    Although it doesn't direclty answer the question AND actually is a "link-only" question, it really is a good read. Maybe one could write a proper answer and we accept it? – Xan-Kun Clark-Davis Nov 26 '18 at 22:21
2

You can read the Name from WindowsIdentity:

var user = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
return Ok(user);
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
Javier Flores
  • 597
  • 4
  • 7
  • Yo get the Identity of the running process on server? – Kiquenet Dec 20 '20 at 10:47
  • This only works if you use impersonation. See also https://learn.microsoft.com/en-us/previous-versions/aspnet/xh507fc5(v=vs.100). If you have a reason to do so, then it is correct, but otherwise, you should probably not use impersonation. – Florian Winter Jan 14 '21 at 08:58
0

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.

0

Username you get like this:

var userName = HttpContext.Current.Request.LogonUserIdentity?.Name;
Ogglas
  • 62,132
  • 37
  • 328
  • 418
-1

declare below code in your View page / _Layout.cshtml:

@using System.DirectoryServices.AccountManagement

@{
    var context = new PrincipalContext(ContextType.Domain);
    var principal = UserPrincipal.FindByIdentity(context, User.Identity.Name);
    var userName = @principal.GivenName + " " + @principal.Surname;
}
janw
  • 8,758
  • 11
  • 40
  • 62
Satish
  • 1