HttpListenerContext.User Property

Definition

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

C#
public System.Security.Principal.IPrincipal? User { get; }
C#
public System.Security.Principal.IPrincipal User { get; }

Property Value

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

Examples

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

C#
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;
}

Remarks

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.

Applies to

Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1