IdentityServer3从客户端请求上下文

时间:2016-10-12 13:39:35

标签: c# ssl owin identityserver3 client-certificates

我有以下配置:一些在iis上托管的网络应用程序,使用"需要SSL"参数集。我也有基于IdentityServer3的身份验证服务。

我需要在我的网络应用程序中将Request.ClientCertificate.SerialNumber传递给身份验证流程中的IdentityServer。

这是我客户端配置的一部分:

Notifications = new OpenIdConnectAuthenticationNotifications
            {
                RedirectToIdentityProvider = n =>
                {
                    // if signing in, send certificate parameters
                    if(n.ProtocolMessage.RequestType == OpenIdConnectRequestType.AuthenticationRequest)
                    {
                        // here i would like to get client certificate
                        var req = n.OwinContext.Request;

                        // and pass it's serial number to IdentityServer someway
                        n.ProtocolMessage.AcrValues = req.ClientCertificate.SerialNumber
                    }

                    return Task.FromResult(0);
                },
          }

有可能吗?我如何获得当前请求的ClientCertificate?

1 个答案:

答案 0 :(得分:0)

最后,我有这个工作:

Notifications = new OpenIdConnectAuthenticationNotifications
{
    RedirectToIdentityProvider = n =>
    {
        // if signing in, send certificate parameters
        if(n.ProtocolMessage.RequestType == OpenIdConnectRequestType.AuthenticationRequest)
        {
            var RequestContext = n.OwinContext.Environment["System.Web.Routing.RequestContext"] as System.Web.Routing.RequestContext;
            if (RequestContext != null)
            {
                var clientCert = RequestContext.HttpContext.Request.ClientCertificate;

                // if client authenticated with certificate then extract certificate info and pass it to identity server
                if (!string.IsNullOrEmpty(clientCert.SerialNumber))
                {
                    var sn = clientCert.SerialNumber.Replace("-", "");

                    // Acr on IdentityServer side explodes by spaces. To prevent splitting values with spaces made some replaces
                    n.ProtocolMessage.AcrValues = "cert:" + sn + " " + clientCert.Subject.Replace(" ","_*_").Replace(",_*_"," ");
                }
            }
        }

        return Task.FromResult(0);
    },
}