本文整理匯總了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;
}