使用Owin进行客户端证书身份验证

时间:2015-08-06 15:09:48

标签: c# owin client-certificates

我尝试使用OWIN管道实现客户端证书身份验证,并且根据2013年底的Dominick Baier博客(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(ticket);
    }
}

问题是GetClaimsFromCertificate不再出现在基类或ClaimsIdentity类中。我猜它感动了,但是我在所有明显的地方都进行了搜索并画了一个空白。有谁知道这个有用的方法发生了什么?

1 个答案:

答案 0 :(得分:1)

您可以尝试使用Thinktecture.IdentityModel Nuget包并使用以下代码创建身份:

var identity = Identity.CreateFromCertificate(cert, Options.AuthenticationType, Options.CreateExtendedClaimSet);

http://referencesource.microsoft.com/#System.IdentityModel/System/IdentityModel/X509Util.cs处的Mi中记录的X509Util类上有一个GetClaimsFromCertificate,但它是内部的:(