我想在没有https的IIS上运行的web.api应用程序中使用客户端身份验证证书。 这可以通过WCF来实现,但似乎无法通过web.api来实现。 以下文章对此进行了明确说明,但我想了解为什么无法做到这一点。
http://southworks.com/blog/2014/06/16/enabling-ssl-client-certificates-in-asp-net-web-api/
注意:我了解为什么永远不要在生产中执行此操作。 要求是在没有SSL的测试环境中运行服务。
谢谢
是的,如果让应用程序验证证书而不是IIS,则有可能。
确保在SSL设置下的IIS中将“客户端证书”设置为“接受”。 这将确保证书被传递到应用程序。 不需要勾选“需要SSL”。
然后,您可以在代码中实现自己的证书验证。 例如: http : //leastprivilege.com/2013/11/11/client-certificate-authentication-middleware-for-katana
public class ClientCertificateAuthenticationHandler :
AuthenticationHandler<ClientCertificateAuthenticationOptions>
{
protected override Task<AuthenticationTicket> AuthenticateCoreAsync()
{
var cert = Context.Get<X509Certificate2>(“ssl.ClientCertificate”);
if (cert == null)
{
return Task.FromResult<AuthenticationTicket>(null);
}
try
{
Options.Validator.Validate(cert);
}
catch
{
return Task.FromResult<AuthenticationTicket>(null);
}
var claims = GetClaimsFromCertificate(
cert, cert.Issuer, Options.CreateExtendedClaimSet);
var identity = new ClaimsIdentity(Options.AuthenticationType);
identity.AddClaims(claims);
var ticket = new AuthenticationTicket(
identity, new AuthenticationProperties());
return Task.FromResult<AuthenticationTicket>(ticket);
}
}