Share via


HttpListenerContext.User Propriedade

Definição

Obtém um objeto usado para obter a identidade, informações de autenticação e funções de segurança para o cliente cuja solicitação é representada por este objeto HttpListenerContext.

public:
 property System::Security::Principal::IPrincipal ^ User { System::Security::Principal::IPrincipal ^ get(); };
public System.Security.Principal.IPrincipal? User { get; }
public System.Security.Principal.IPrincipal User { get; }
member this.User : System.Security.Principal.IPrincipal
Public ReadOnly Property User As IPrincipal

Valor da propriedade

Um objeto IPrincipal que descreve o cliente ou null, se o HttpListener que forneceu este HttpListenerContext não exigir autenticação.

Exemplos

O exemplo de código a seguir demonstra como acessar informações de identidade e autenticação sobre o cliente e devolvê-lo ao cliente na resposta.

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 Shared Function ClientInformation(ByVal context As HttpListenerContext) As String
    Dim user As System.Security.Principal.IPrincipal = context.User
    Dim id As System.Security.Principal.IIdentity = user.Identity

    If id Is Nothing Then
        Return "Client authentication is not enabled for this Web server."
    End If

    Dim display As String

    If id.IsAuthenticated Then
        display = String.Format("{0} was authenticated using {1}", id.Name, id.AuthenticationType)
    Else
        display = String.Format("{0} was not authenticated", id.Name)
    End If

    Return display
End Function

Comentários

Um HttpListener indica que ele requer autenticação usando a AuthenticationSchemes propriedade ou especificando um AuthenticationSchemeSelector delegado usando a AuthenticationSchemeSelectorDelegate propriedade .

Para determinar o nome de logon e as informações de autenticação do cliente, marcar a IPrincipal.Identity propriedade no objeto retornado por essa propriedade.

Aplica-se a