HttpContext.User HttpContext.User HttpContext.User HttpContext.User Property


为当前 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


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

由于System.Web ASP.NET 页包含对命名空间 ( HttpContext包含类) 的默认引用, 因此你HttpContext可以在不使用对HttpContext的完全限定类引用的情况下, 引用 .aspx 页上的成员。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. 但是, 如果要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.