Get Windows Username in ASP.NET without System.Web

怎甘沉沦 提交于 2019-12-02 06:09:22

问题


I have a reporting dll that is used in ASP.NET (Windows Authentication) and winforms projects. Sometimes it needs the username so if it was running in ASP.NET it would use:

System.Web.HttpContext.Current.User.Identity.Name;

and in windows

WindowsIdentity.GetCurrent().Name

Now I am trying to move to .NET 4 Client Profile so I need to avoid the System.Web reference in the reporting dll. Is there an alternative way to get the Windows Username for when the dll is running in ASP.NET which won't use System.Web. The only way I can currently see to avoid System.Web is to pass the Username as a parameter which will be a pain to adjust for in every report.


回答1:


Maybe you can use the CurrentPrincipal property of the Thread class:

using System.Threading;

string userName = Thread.CurrentPrincipal.Identity.Name;



回答2:


You can use Environment.UserName which lives in System.




回答3:


System.Web namespace is definitely available in an ASP.Net application. You can definitely make use of it. Apart from that, you will also have the Membership providers that you can use to validate or tell the current user name.




回答4:


Using System.net -- NetworkCredential.Username?

Ref: http://msdn.microsoft.com/en-us/library/system.net.networkcredential.username.aspx




回答5:


If Windows authentication is on this might work: Page.User.Identity.Name In my case, it does.



来源:https://stackoverflow.com/questions/6748976/get-windows-username-in-asp-net-without-system-web

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