简体   繁体   English

在没有 System.Web 的 ASP.NET 中获取 Windows 用户名

[英]Get Windows Username in ASP.NET without System.Web

I have a reporting dll that is used in ASP.NET (Windows Authentication) and winforms projects.我有一个报告 dll 用于 ASP.NET(Windows 身份验证)和 winforms 项目。 Sometimes it needs the username so if it was running in ASP.NET it would use:有时它需要用户名,所以如果它在 ASP.NET 中运行,它将使用:

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

and in windows并在 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.现在我正在尝试迁移到 .NET 4 客户端配置文件,因此我需要避免报告 dll 中的 System.Web 引用。 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.当 dll 在 ASP.NET 中运行时,是否有另一种方法来获取 Windows 用户名,它不会使用 System.ZC34CCE190B25804633。 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.我目前可以看到避免 System.Web 的唯一方法是将用户名作为参数传递,这在每个报告中都很难调整。

Maybe you can use the CurrentPrincipal property of the Thread class:也许您可以使用线程class 的CurrentPrincipal属性:

using System.Threading;

string userName = Thread.CurrentPrincipal.Identity.Name;

You can use Environment.UserName which lives in System.您可以使用位于系统中的 Environment.UserName。

If Windows authentication is on this might work: Page.User.Identity.Name In my case, it does.如果 Windows 身份验证启用,这可能有效: Page.User.Identity.Name在我的情况下,它确实有效。

System.Web namespace is definitely available in an ASP.Net application. System.Web 命名空间在 ASP.Net 应用程序中绝对可用。 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.除此之外,您还将拥有可用于验证或告知当前用户名的成员资格提供程序。

Using System.net -- NetworkCredential.Username?使用 System.net -- NetworkCredential.Username?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM