2016-10-12 6 views
0

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

Мне нужно передать 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 текущего запроса?

+0

См https://leastprivilege.com/2013/11/11/client-certificate-authentication-middleware-for-katana/ для примера работа с сертификатами клиентов. – Tratcher

ответ

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