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.


下面的示例演示如何通过 User 属性访问当前用户的属性。The 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 属性提供对 IPrincipal 接口的属性和方法的编程访问。The User property provides programmatic access to the properties and methods of the IPrincipal interface.

由于 ASP.NET 页包含对 System.Web 命名空间(包含 HttpContext 类)的默认引用,因此你可以在不使用对 HttpContext的完全限定类引用的情况下引用 .aspx 页上 HttpContext 的成员。Because 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. 但是,如果要从 ASP.NET 代码隐藏模块使用 IPrincipal 的成员,则必须在模块中包括对 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.Name的完全限定名称。For example, in a code-behind page you must specify the fully qualified name HttpContext.Current.User.Identity.Name.