IdentityServer3 передает запрос.ClientCertificate из контекста запроса клиента

#c# #ssl #owin #identityserver3 #client-certificates

#c# #ssl #owin #identityserver3 #клиент-сертификаты

Вопрос:

У меня следующая конфигурация: некоторое веб-приложение, размещенное в iis, с набором параметров «Требовать SSL». Также у меня есть служба аутентификации, основанная на IdentityServer3.

Мне нужно передать запрос.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. См . leastprivilege.com/2013/11/11 /… для примера работы с клиентскими сертификатами.

Ответ №1:

Наконец, у меня это работает:

 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);
    },
}