Export (0) Print
Expand All

HttpListenerContext.User Property

Gets an object used to obtain identity, authentication information, and security roles for the client whose request is represented by this HttpListenerContext object.

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

public IPrincipal User { get; }

Property Value

Type: System.Security.Principal.IPrincipal
An IPrincipal object that describes the client, or null if the HttpListener that supplied this HttpListenerContext does not require authentication.

An HttpListener indicates that it requires authentication using the AuthenticationSchemes property or by specifying an AuthenticationSchemeSelector delegate using the AuthenticationSchemeSelectorDelegate property.

To determine the client's login name and authentication information, check the IPrincipal.Identity property in the object returned by this property.

The following code example demonstrates accessing identity and authentication information about the client, and returning it to the client in the response.

public static string ClientInformation(HttpListenerContext context)
{
    System.Security.Principal.IPrincipal user = context.User;
    System.Security.Principal.IIdentity id = user.Identity;
    if (id == null)
    {
        return "Client authentication is not enabled for this Web server.";
    }

    string display;
    if (id.IsAuthenticated)
    {
        display = String.Format("{0} was authenticated using {1}", id.Name, 
            id.AuthenticationType);
    }
    else
    {
       display = String.Format("{0} was not authenticated", id.Name);
    }
    return display;
}

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0

Community Additions

ADD
Show:
© 2014 Microsoft