I can't figure this out for the life of me. I'm trying to get the name of the current user logged onto Windows using the following line:
string user = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();
When I run this line after publishing and opening it through IIS, it gives me a name of "IIS APPPOOL/SiteName". However, when I run this through the Visual Studio 2013 debugger, the correct name appears.
you have to enable windows auth/impersonation on an ASP.NET site, else it will run in the context of the whatever account configured for the app pool.
https://msdn.microsoft.com/en-us/library/ff647405.aspx
<system.web>
...
<authentication mode="Windows"/>
<identity impersonate="true"/>
...
</system.web>
Base on my test under IIS having Windows Authentication only enable and not impersonation on the web.config;
System.Web.HttpContext.Current.User.Identity.Name;
return to me the current login user not the application pool user and System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString()
return the application pool user.
Hope this help!
I've fiddled around with the config, IIS settings, and the string...but I think this line is what I needed to use:
string user = System.Web.HttpContext.Current.User.Identity.Name;
Seems to be returning a domain/username which I can use instead. Looks like an alternative solution.
All mentioned in other answers are true, PLUS THIS:
In IIS Manager, click Basic Settings.
In the Edit Application window click Connect as...
Choose Application User (pass through authentication). Do not use a specific user because that will be the identity detected.
Click on the project name and press F4 and it will open project properties window:
Enable Windows authentication
Disable anonymous authentication
- Add
<identity impersonate="true">
in web.config
Now, deploy your code it should work fine.
来源:https://stackoverflow.com/questions/30226024/getting-the-name-of-the-current-windows-user-returning-iis-apppool-sitename