Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

HttpContext.User Property

Gets or sets security information for the current HTTP request.

Namespace:  System.Web
Assembly:  System.Web (in System.Web.dll)

public IPrincipal User { get; set; }

Property Value

Type: System.Security.Principal.IPrincipal
Security information for the current HTTP request.

The User property provides programmatic access to the properties and methods of the IPrincipal interface.

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. For example, you can use User.Identity.Name to get the name of the user on whose behalf the current process is running. 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. For example, in a code-behind page you must specify the fully qualified name HttpContext.Current.User.Identity.Name.

TopicLocation
How to: Implement Passport Authentication in ASP.NET ApplicationsBuilding ASP .NET Web Applications

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.

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;
    }
    else
    {
        Page.Title = "Home page for guest user.";
    }
}

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1
Show:
© 2015 Microsoft