HttpListenerContext.User 屬性

定義

取得用來取得用戶端識別、驗證資訊和安全性角色之物件,由這個 HttpListenerContext 物件表示用戶端的要求。Gets an object used to obtain identity, authentication information, and security roles for the client whose request is represented by this HttpListenerContext object.

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

屬性值

描述用戶端的 IPrincipal 物件,如果提供這個 nullHttpListener 不需要驗證,則為 HttpListenerContextAn IPrincipal object that describes the client, or null if the HttpListener that supplied this HttpListenerContext does not require authentication.

範例

下列程式碼範例將示範如何存取用戶端的身分識別和驗證資訊,並在回應中將它傳回給用戶端。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 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

備註

HttpListener 表示它需要使用 AuthenticationSchemes 屬性進行驗證,或使用 AuthenticationSchemeSelectorDelegate 屬性指定 AuthenticationSchemeSelector 委派。An HttpListener indicates that it requires authentication using the AuthenticationSchemes property or by specifying an AuthenticationSchemeSelector delegate using the AuthenticationSchemeSelectorDelegate property.

若要判斷用戶端的登入名稱和驗證資訊,請檢查此屬性所傳回之物件中的 IPrincipal.Identity 屬性。To determine the client's login name and authentication information, check the IPrincipal.Identity property in the object returned by this property.

安全性

SecurityPermission
相關聯的列舉: ControlPrincipalUnmanagedCodeAssociated enumeration: ControlPrincipalUnmanagedCode

適用於