共用方式為


HttpListenerContext.User 屬性

定義

取得用來取得用戶端識別、驗證資訊和安全性角色之物件,由這個 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

屬性值

描述用戶端的 IPrincipal 物件,如果提供這個 HttpListenerHttpListenerContext 不需要驗證,則為 null

範例

下列程式代碼範例示範如何存取用戶端的身分識別和驗證資訊,並在回應中將其傳回至用戶端。

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表示它需要使用 屬性或藉由使用 AuthenticationSchemeSelectorDelegate 屬性指定AuthenticationSchemeSelector委派來進行驗證AuthenticationSchemes

若要判斷用戶端的登入名稱和驗證資訊,請檢查 IPrincipal.Identity 此屬性所傳回之 物件中的 屬性。

適用於