Getting the name of the current Windows user returning “IIS APPPOOL/Sitename”

流过昼夜 提交于 2019-11-28 07:31:54

问题


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.


回答1:


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>



回答2:


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!




回答3:


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.




回答4:


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.




回答5:


Click on the project name and press F4 and it will open project properties window:

  1. Enable Windows authentication

  2. Disable anonymous authentication

  3. 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!