Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

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 */
public IPrincipal get_User ()

public function get User () : IPrincipal

Not applicable.

Property Value

An IPrincipal object that describes the client, or a null reference (Nothing in Visual Basic) 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;
}

public static String ClientInformation(HttpListenerContext context)
{
    System.Security.Principal.IPrincipal user = context.get_User();
    System.Security.Principal.IIdentity id = user.get_Identity();
    if (id == null) {
        return "Client authentication is not enabled for this Web server.";
    }
    String display;
    if (id.get_IsAuthenticated()) {
        display = String.Format("{0} was authenticated using {1}", 
            id.get_Name(), id.get_AuthenticationType());
    }
    else {
        display = String.Format("{0} was not authenticated", id.get_Name());
    }
    return display;
} //ClientInformation

Windows 98, Windows Server 2003, Windows XP Media Center Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft