Search code examples
asp.netwindowspermissionslogin-name

How to get currently logged in Windows user ID on client side


I have a web application that allows the users to access the pages if their Windows login name to the domain is in the web config's app settings.

For example: Logged on username: SampleDomain\SampleUser

on Web.config:

<appSettings>
    <add key=AuthorizedUsers value="SampleUser,SampleBooger" />
</appSettings>
<authentication mode="Windows"/>

*successful scenario: SampleUser should be able to browse the website.

I was able to do it successfully during development (locally on my machine), but when I deployed it to a server then I try to browse it from my local machine, the Windows login that it gets is NT AUTHORITY\NETWORK SERVICE so it redirects to my customized error page. I, SampleUser, am in the AuthorizedUsers. I should be able to browse it.

Please help me.

Thank you very much.


Solution

  • You need to use ASP.NET impersonation feature. When using impersonation, ASP.NET applications can execute with the Windows identity (user account) of the user making the request. Impersonation is commonly used in applications that rely on Microsoft Internet Information Services (IIS) to authenticate the user.

    Such behavior can be configured in web config using the following code:

    <configuration>
      <system.web>
        <identity impersonate="true"/>
      </system.web>
    </configuration>
    

    More info: http://msdn.microsoft.com/en-us/library/xh507fc5%28v=vs.100%29.aspx