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 類別),因此您可以參考 .aspx 頁面上 HttpContext 的成員,而不需使用 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 命名空間的參考,以及目前使用中要求/回應內容和類別的完整參考 @no__t您想要使用的 2However, 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.