让Windows用户名在ASP.NET中没有的System.Web [英] Get Windows Username in ASP.NET without System.Web
问题描述
我有一个在ASP.NET(Windows身份验证)和WinForms项目中使用报表DLL。有时,它需要用户名,所以如果它是在ASP.NET运行它会用:
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;
和Windows
and in windows
WindowsIdentity.GetCurrent().Name
现在我想移动到.NET 4客户端配置文件,所以我需要避免在报告的dll对System.Web引用。是否有其他方式获得的Windows用户名时,则DLL在ASP.NET将不使用的System.Web运行。我现在可以看到,以避免的System.Web的唯一方法是通过用户名的,这将是一个痛苦的每一份报告来调整参数。
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.
推荐答案
也许你可以使用的 CurrentPrincipal 上的主题类:
Maybe you can use the CurrentPrincipal property of the Thread class:
using System.Threading;
string userName = Thread.CurrentPrincipal.Identity.Name;
这篇关于让Windows用户名在ASP.NET中没有的System.Web的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!