-1

Is it possible to get the Windows username from a client that is sending a request to the server hosting the site, without the user having to enter it into a login field?

I am building a website that is public facing and my managers want anyone on our network where the site is hosted to not have to login when accessing the site. They want the site to just automatically get the Windows username and authenticate it against Active Directory when they access the site.

I have been playing around with stuff like Request.LogonUserIdentity.Name and WindowsPrincipal(WindowsIdentity.Name), but they always return the IIS app pool name. I have tried every combination of authentication in IIS.

The HttpContext.User.Identity.Name is always blank when I start the site and check it in the home controller, but it is populated after I try to access an area of the site and use the authentication that brings up a login box where the user has to enter their Windows credentials.

Is there no way to just automatically get the Windows username without having to prompt the user for it?

************************************************ SOLVED ****************************************************

I had all the setting set correctly. The only thing I was doing wrong was using Firefox. Internet Explorer is the only browser that will automatically retrieve the user's windows account and populate the User.Identity object. Any other browser can populate this, but only after prompting the user for their Windows account credentials.

ChumpStick
  • 51
  • 11

1 Answers1

1

Go to the authentication settings in IIS and set your site up with Windows Authentication if it isn't already. Make sure your web config is set correctly as well with this element:

<system.web>
    <authentication mode="Windows" />
    <authorization>
        <allow users="*" />
        <deny users="?"/>
    </authorization>
</system.web>

You shouldn't be getting a login prompt using Windows Authentication, so see this answer if you are. Then you should hopefully be able to get the user with:

System.Web.HttpContext.Current.User
  • Yes that gets rid of the login popup, but the HttpContext.Current.User is still null. I've only ever seen that object populated after the user enters their credentials in the login popup. That's why I am asking if there is a way to get the Windows username without the popup. – ChumpStick Feb 02 '18 at 15:51
  • Make 100% sure you have the authentication mode set to "Windows" In your web.config like above. Also, make sure IIS only has Windows Authentication enabled for your site. If you have anonymous authentication enabled then it wont work – Brandon Church Feb 02 '18 at 15:56
  • I copied exactly what you have there into my web config... published it... disabled all authentication modes except for Windows in IIS... It prompts me for a user name and password when I try to view the site. – ChumpStick Feb 06 '18 at 14:18
  • Okay.. Try adding "Authenticated Users" or yourself to the security settings on the actual application directory. – Brandon Church Feb 06 '18 at 14:45
  • What do you mean by that? – ChumpStick Feb 08 '18 at 17:30