HttpListenerContext.User 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取用于为客户端获取标识、身份验证信息和安全角色的对象,该客户端的请求由此 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 对象,或者,如果提供此 HttpListener 的 HttpListenerContext 不需要身份验证,则为 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 它需要使用 AuthenticationSchemes 属性或通过使用 属性指定 AuthenticationSchemeSelector 委托 AuthenticationSchemeSelectorDelegate 进行身份验证。
若要确定客户端的登录名和身份验证信息,请检查 IPrincipal.Identity 此属性返回的 对象中的 属性。