本文整理汇总了C#中System.Net.Http.HttpRequestMessage.GetClientCertificate方法的典型用法代码示例。如果您正苦于以下问题:C# HttpRequestMessage.GetClientCertificate方法的具体用法?C# HttpRequestMessage.GetClientCertificate怎么用?C# HttpRequestMessage.GetClientCertificate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.Http.HttpRequestMessage
的用法示例。
在下文中一共展示了HttpRequestMessage.GetClientCertificate方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SendAsync
protected override async Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request, CancellationToken cancellationToken)
{
var cert = request.GetClientCertificate();
if (cert == null) return await base.SendAsync(request, cancellationToken);
try
{
_validator.Validate(cert);
}
catch (SecurityTokenValidationException)
{
return new HttpResponseMessage(HttpStatusCode.Unauthorized);
}
var issuer = _issuerMapper(cert);
if (issuer == null)
{
return new HttpResponseMessage(HttpStatusCode.Unauthorized);
}
var claims = ExtractClaims(cert, issuer);
var identity = new ClaimsIdentity(new ClaimsIdentity(claims, X509AuthnMethod));
AddIdentityToCurrentPrincipal(identity, request);
return await base.SendAsync(request, cancellationToken);
}
示例2: SendAsync
protected override Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request,
CancellationToken cancellationToken)
{
var cert = request.GetClientCertificate();
if (cert != null)
{
// Not sure how this ever results in not null. Hope to find out
}
return base.SendAsync(request, cancellationToken);
}
示例3: SendAsync
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
// Retrieving the Client certificate sent by the client
X509Certificate cert = request.GetClientCertificate();
// The following code SHOULD be replaced with some custom mapping logic from client certificate to their roles.
// The client certificate is not being checked for certificate revocation or ensure PKI validity.
if (cert != null)
{
if (cert.GetCertHashString() == Program.ClientCertHash)
{
Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity(cert.Subject), new[] { "Administrators" });
}
}
return base.SendAsync(request, cancellationToken);
}
示例4: SendAsync
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
// Retrieving the Client certificate sent by the client
X509Certificate cert = request.GetClientCertificate();
// The following code SHOULD be replaced with some custom mapping logic from client certificate to their roles.
if (cert != null)
{
if (cert.GetCertHashString() == ClientCertHash)
{
Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity(cert.Subject, "X509Certificate"), new[] { "Administrators" });
if (HttpContext.Current != null)
{
HttpContext.Current.User = Thread.CurrentPrincipal;
}
}
}
return base.SendAsync(request, cancellationToken);
}
示例5: Authenticate
public ClaimsPrincipal Authenticate(HttpRequestMessage request)
{
string resourceName = request.RequestUri.AbsoluteUri;
// if session feature is enabled (and this is not a token request), check for session token first
if (Configuration.EnableSessionToken && !IsSessionTokenRequest(request))
{
var principal = AuthenticateSessionToken(request);
if (principal.Identity.IsAuthenticated)
{
Tracing.Information(Area.HttpAuthentication, "Client authenticated using session token");
return principal;
}
}
// check for credentials on the authorization header
if (Configuration.HasAuthorizationHeaderMapping)
{
var authZ = request.Headers.Authorization;
if (authZ != null)
{
Tracing.Verbose(Area.HttpAuthentication, "Mapping for authorization header found: " + authZ.Scheme);
var principal = AuthenticateAuthorizationHeader(authZ.Scheme, authZ.Parameter);
if (principal.Identity.IsAuthenticated)
{
Tracing.Information(Area.HttpAuthentication, "Client authenticated using authorization header mapping: " + authZ.Scheme);
return Transform(resourceName, principal);
}
}
}
// check for credentials on other headers
if (Configuration.HasHeaderMapping)
{
if (request.Headers != null)
{
Tracing.Verbose(Area.HttpAuthentication, "Mapping for header header found.");
var principal = AuthenticateHeaders(request.Headers);
if (principal.Identity.IsAuthenticated)
{
Tracing.Information(Area.HttpAuthentication, "Client authenticated using header mapping");
return Transform(resourceName, principal);
}
}
}
// check for credentials on the query string
if (Configuration.HasQueryStringMapping)
{
if (request.RequestUri != null && !string.IsNullOrWhiteSpace(request.RequestUri.Query))
{
Tracing.Verbose(Area.HttpAuthentication, "Mapping for query string found.");
var principal = AuthenticateQueryStrings(request.RequestUri);
if (principal.Identity.IsAuthenticated)
{
Tracing.Information(Area.HttpAuthentication, "Client authenticated using query string mapping");
return Transform(resourceName, principal);
}
}
}
// check for client certificate
if (Configuration.HasClientCertificateMapping)
{
var cert = request.GetClientCertificate();
if (cert != null)
{
Tracing.Verbose(Area.HttpAuthentication, "Mapping for client certificate found.");
var principal = AuthenticateClientCertificate(cert);
if (principal.Identity.IsAuthenticated)
{
Tracing.Information(Area.HttpAuthentication, "Client authenticated using client certificate");
return Transform(resourceName, principal);
}
}
}
// do claim transformation (if enabled), and return.
return Transform(resourceName, Principal.Anonymous);
}
示例6: TryGetClientCertificateFromRequest
public bool TryGetClientCertificateFromRequest(HttpRequestMessage request, out X509Certificate2 clientCertificate)
{
clientCertificate = request.GetClientCertificate();
if (clientCertificate != null)
{
return true;
}
return false;
}