How to get user name using Windows authentication in asp.net?

user2148679 picture user2148679 · Oct 30, 2013 · Viewed 116.9k times · Source

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>

.cs

Here I am getting the default User name always

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

Any ideas? Thanks in advance

Answer

slepox picture slepox · Oct 30, 2013

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.