HttpContext.User 属性


获取或设置当前 HTTP 请求的安全信息。Gets or sets security information for the current HTTP request.

 property System::Security::Principal::IPrincipal ^ User { System::Security::Principal::IPrincipal ^ get(); void set(System::Security::Principal::IPrincipal ^ value); };
public System.Security.Principal.IPrincipal User { get; set; }
member this.User : System.Security.Principal.IPrincipal with get, set
Public Property User As IPrincipal



当前 HTTP 请求的安全信息。Security information for the current HTTP request.


下面的示例演示如何通过属性访问当前用户的属性 UserThe following example shows how to access properties of the current user through the User property. 这些属性用于设置网页标题。Those properties are used to set the title of the Web page.

如果应用程序使用 Windows 身份验证,则用户名将包括域。If the application uses Windows authentication, the user name includes the domain. 例如,页标题为 "域 \ 用户名的主页"。For example, the page title would be "Home page for DOMAIN\username".

protected void Page_Load(object sender, EventArgs e)
    if (User.Identity.IsAuthenticated)
        Page.Title = "Home page for " + User.Identity.Name;
        Page.Title = "Home page for guest user.";
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If (User.Identity.IsAuthenticated) Then
        Page.Title = "Home page for " + User.Identity.Name
        Page.Title = "Home page for guest user."
    End If
End Sub


User属性提供对接口的属性和方法的编程访问 IPrincipalThe User property provides programmatic access to the properties and methods of the IPrincipal interface.

由于 ASP.NET 页包含对 System.Web 包含) 类 (命名空间的默认引用 HttpContext ,因此可以 HttpContext 在不使用对的完全限定类引用的情况下引用 .aspx 页上的成员 HttpContextBecause ASP.NET pages contain a default reference to the System.Web namespace (which contains the HttpContext class), you can reference the members of HttpContext on an .aspx page without using the fully qualified class reference to HttpContext. 例如,可以使用 User.Identity.Name 获取当前进程正在其上运行的用户的名称。For example, you can use User.Identity.Name to get the name of the user on whose behalf the current process is running. 但是,如果你想要 IPrincipal 从 ASP.NET 代码隐藏模块使用的成员,则必须 System.Web 在模块中包括对命名空间的引用,以及对当前活动的请求/响应上下文和 System.Web 你要使用的中的类的完全限定引用。However, if you want to use the members of IPrincipal from an ASP.NET code-behind module, you must include a reference to the System.Web namespace in the module and a fully qualified reference to both the currently active request/response context and the class in System.Web that you want to use. 例如,在代码隐藏页中,必须指定完全限定的名称 HttpContext.Current.User.Identity.NameFor example, in a code-behind page you must specify the fully qualified name HttpContext.Current.User.Identity.Name.