3
votes

I'm working on an ASP.NET MVC app running on our intranet. We are using forms authentication, however the usernames set up will be the same as the Active Directory usernames. I'm trying to find a way to pre-populate the login username field with the Windows username, but can't find a way to access the current Windows user identity.

System.Environment.UserDomainName returns "IIS APPPOOL"

System.Security.Principal.WindowsPrincipal.Current.Identity returns a GenericIdentity

HttpContext.Request.LogonUserIdentity.Name returns "NT AUTHORITY\IUSR"

I do not want to authenticate the Windows user or login to the website as the Windows user, I just want to read the username. Is there any way to do this whilst using Forms Authentication rather than Windows Authentication?

2
I think adding this <identity impersonate="true" /> to the webconfig (just beneath the <authentication> bit, not inside!) should do what you need. Then the Controller should have a User property you can query, rather than the code above... - Tallmaris
I just tried it and got Principal.GenericIdentity - TabbyCool

2 Answers

0
votes

Try this please:

In the web.config

<identity impersonate="true" />
<authentication mode="Forms">
     <forms loginUrl="./WebLogin.aspx" defaultUrl="./default.aspx" timeout="12" path="/" name=".ASPXFORMSAUTH" />
</authentication>

In the code c#

string windowsName = WindowsPrincipal.Current.Identity.Name;
//OR
string windowsName = Thread.CurrentPrincipal.Identity.Name;

This work fine for me.

0
votes

Turn off the Windows Authentification in the IIS then should everything work possible