如何在C#中找到用户名/身份

时间:2021-10-28 08:51:13

I need to programatically find the users name using C#. Specifically, I want to get the system/network user attached to the current process. I'm writing a web application that uses windows integrated security.

我需要使用C#以编程方式查找用户名。具体来说,我想让系统/网络用户连接到当前进程。我正在编写一个使用Windows集成安全性的Web应用程序。

3 个解决方案

#1


35  

The abstracted view of identity is often the IPrincipal/IIdentity:

抽象的身份观通常是IPrincipal / IIdentity:

IPrincipal principal = Thread.CurrentPrincipal;
IIdentity identity = principal == null ? null : principal.Identity;
string name = identity == null ? "" : identity.Name;

This allows the same code to work in many different models (winform, asp.net, wcf, etc) - but it relies on the identity being set in advance (since it is application-defined). For example, in a winform you might use the current user's windows identity:

这允许相同的代码在许多不同的模型(winform,asp.net,wcf等)中工作 - 但它依赖于事先设置的身份(因为它是应用程序定义的)。例如,在winform中,您可以使用当前用户的窗口标识:

Thread.CurrentPrincipal = new WindowsPrincipal(WindowsIdentity.GetCurrent());

However, the principal can also be completely bespoke - it doesn't necessarily relate to windows accounts etc. Another app might use a login screen to allow arbitrary users to log on:

但是,委托人也可以完全定制 - 它不一定与Windows帐户等有关。另一个应用程序可能使用登录屏幕允许任意用户登录:

string userName = "Fred"; // todo
string[] roles = { "User", "Admin" }; // todo
Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity(userName), roles);

#2


16  

Depends on the context of the application. You can use Environment.UserName (console) or HttpContext.Current.User.Identity.Name (web). Note that when using Windows integrated authentication, you may need to remove the domain from the user name. Also, you can get the current user using the User property of the page in codebehind, rather than referencing it from the current HTTP context.

取决于应用程序的上下文。您可以使用Environment.UserName(控制台)或HttpContext.Current.User.Identity.Name(web)。请注意,使用Windows集成身份验证时,可能需要从用户名中删除域。此外,您可以使用代码隐藏中的页面的User属性来获取当前用户,而不是从当前HTTP上下文中引用它。

#3


3  

string user = System.Security.Principal.WindowsIdentity.GetCurrent().Name ;

#1


35  

The abstracted view of identity is often the IPrincipal/IIdentity:

抽象的身份观通常是IPrincipal / IIdentity:

IPrincipal principal = Thread.CurrentPrincipal;
IIdentity identity = principal == null ? null : principal.Identity;
string name = identity == null ? "" : identity.Name;

This allows the same code to work in many different models (winform, asp.net, wcf, etc) - but it relies on the identity being set in advance (since it is application-defined). For example, in a winform you might use the current user's windows identity:

这允许相同的代码在许多不同的模型(winform,asp.net,wcf等)中工作 - 但它依赖于事先设置的身份(因为它是应用程序定义的)。例如,在winform中,您可以使用当前用户的窗口标识:

Thread.CurrentPrincipal = new WindowsPrincipal(WindowsIdentity.GetCurrent());

However, the principal can also be completely bespoke - it doesn't necessarily relate to windows accounts etc. Another app might use a login screen to allow arbitrary users to log on:

但是,委托人也可以完全定制 - 它不一定与Windows帐户等有关。另一个应用程序可能使用登录屏幕允许任意用户登录:

string userName = "Fred"; // todo
string[] roles = { "User", "Admin" }; // todo
Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity(userName), roles);

#2


16  

Depends on the context of the application. You can use Environment.UserName (console) or HttpContext.Current.User.Identity.Name (web). Note that when using Windows integrated authentication, you may need to remove the domain from the user name. Also, you can get the current user using the User property of the page in codebehind, rather than referencing it from the current HTTP context.

取决于应用程序的上下文。您可以使用Environment.UserName(控制台)或HttpContext.Current.User.Identity.Name(web)。请注意,使用Windows集成身份验证时,可能需要从用户名中删除域。此外,您可以使用代码隐藏中的页面的User属性来获取当前用户,而不是从当前HTTP上下文中引用它。

#3


3  

string user = System.Security.Principal.WindowsIdentity.GetCurrent().Name ;