2015-06-17 1 views
3

Я пытаюсь использовать аутентификацию OpenId и Bearer token в своем приложении через Identity Server.Использовать аутентификацию маркера предъявителя для аутентификации API и OpenId для MVC в том же проекте приложения

В настоящее время проблема заключается в том, что, как только я аутентифицировал пользователя, мне все равно нужно получить токен-носитель, чтобы иметь возможность вызывать любые методы действий для моего приложения ASP.NET MVC.

Вот мой файл запуска для приложения

public class Startup 
{ 
    public void Configuration(IAppBuilder app) 
    {    
     AntiForgeryConfig.UniqueClaimTypeIdentifier = Constants.ClaimTypes.Subject; 
     JwtSecurityTokenHandler.InboundClaimTypeMap = new Dictionary<string, string>(); 
     app.UseCookieAuthentication(new CookieAuthenticationOptions 
     { 
      AuthenticationType = "Cookies" 
     }); 

     app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions 
     { 
      Authority = "https://localhost:44301/identity", 
      ClientId = "baseballStats", 
      Scope = "openid profile roles baseballStatsApi", 
      RedirectUri = "https://localhost:44300/", 
      ResponseType = "id_token token", 
      SignInAsAuthenticationType = "Cookies", 
      UseTokenLifetime = false, 
      Notifications = new OpenIdConnectAuthenticationNotifications 
      { 
       SecurityTokenValidated = async n => 
       { 
        var userInfoClient = new UserInfoClient(
           new Uri(n.Options.Authority + "/connect/userinfo"), 
           n.ProtocolMessage.AccessToken); 

        var userInfo = await userInfoClient.GetAsync(); 

        // create new identity and set name and role claim type 
        var nid = new ClaimsIdentity(
         n.AuthenticationTicket.Identity.AuthenticationType, 
         Constants.ClaimTypes.GivenName, 
         Constants.ClaimTypes.Role); 

        userInfo.Claims.ToList().ForEach(c => nid.AddClaim(new Claim(c.Item1, c.Item2))); 

        // keep the id_token for logout 
        nid.AddClaim(new Claim("id_token", n.ProtocolMessage.IdToken)); 

        // add access token for sample API 
        nid.AddClaim(new Claim("access_token", n.ProtocolMessage.AccessToken)); 

        // keep track of access token expiration 
        nid.AddClaim(new Claim("expires_at", DateTimeOffset.Now.AddSeconds(int.Parse(n.ProtocolMessage.ExpiresIn)).ToString())); 

        // add some other app specific claim 
        nid.AddClaim(new Claim("app_specific", "some data")); 

        n.AuthenticationTicket = new AuthenticationTicket(
         nid, 
         n.AuthenticationTicket.Properties); 
       } 
      } 
     }); 



     app.UseResourceAuthorization(new AuthorizationManager()); 

     app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions 
     { 
      Authority = "https://localhost:44301/identity", 
      RequiredScopes = new[] { "baseballStatsApi" } 
     }); 

     var config = new HttpConfiguration(); 
     config.MapHttpAttributeRoutes(); 
     app.UseWebApi(config);   
    } 
} 

Я хотел бы ограничить маркер аутентификации однонаправленного только мои API URLs, и использовать OpenId авторизацию для Everthing еще. Есть ли способ сделать это?

ответ

6

Хорошо, я нашел некоторую информацию о следующем посте

https://github.com/IdentityServer/IdentityServer3/issues/487

GitHub репо, который реализует концепцию, описанную в ссылке можно найти здесь

https://github.com/B3nCr/IdentityServer-Sample/blob/master/B3nCr.Communication/Startup.cs

В основном вам необходимо сопоставить api url с другой конфигурацией, используя app.Map(). В моем случае я изменил файл автозагрузки, чтобы выглядеть так.

public class Startup 
{ 
    public void Configuration(IAppBuilder app) 
    { 
     AntiForgeryConfig.UniqueClaimTypeIdentifier = Thinktecture.IdentityServer.Core.Constants.ClaimTypes.Subject; 
     JwtSecurityTokenHandler.InboundClaimTypeMap = new Dictionary<string, string>(); 

     app.UseCookieAuthentication(new CookieAuthenticationOptions 
     { 
      AuthenticationType = "Cookies" 
     }); 

     var openIdConfig = new OpenIdConnectAuthenticationOptions 
     { 
      Authority = "https://localhost:44301/identity", 
      ClientId = "baseballStats", 
      Scope = "openid profile roles baseballStatsApi", 
      RedirectUri = "https://localhost:44300/", 
      ResponseType = "id_token token", 
      SignInAsAuthenticationType = "Cookies",     
      UseTokenLifetime = false, 
      Notifications = new OpenIdConnectAuthenticationNotifications 
      { 
       SecurityTokenValidated = async n => 
       { 
        var userInfoClient = new UserInfoClient(
            new Uri(n.Options.Authority + "/connect/userinfo"), 
            n.ProtocolMessage.AccessToken); 

        var userInfo = await userInfoClient.GetAsync(); 

        // create new identity and set name and role claim type 
        var nid = new ClaimsIdentity(
         n.AuthenticationTicket.Identity.AuthenticationType, 
         Thinktecture.IdentityServer.Core.Constants.ClaimTypes.GivenName, 
         Thinktecture.IdentityServer.Core.Constants.ClaimTypes.Role); 

        userInfo.Claims.ToList().ForEach(c => nid.AddClaim(new Claim(c.Item1, c.Item2))); 

        // keep the id_token for logout 
        nid.AddClaim(new Claim("id_token", n.ProtocolMessage.IdToken)); 

        // add access token for sample API 
        nid.AddClaim(new Claim("access_token", n.ProtocolMessage.AccessToken)); 

        // keep track of access token expiration 
        nid.AddClaim(new Claim("expires_at", DateTimeOffset.Now.AddSeconds(int.Parse(n.ProtocolMessage.ExpiresIn)).ToString())); 

        // add some other app specific claim 
        nid.AddClaim(new Claim("app_specific", "some data")); 

        n.AuthenticationTicket = new AuthenticationTicket(
         nid, 
         n.AuthenticationTicket.Properties); 

        n.Request.Headers.SetValues("Authorization ", new string[] { "Bearer ", n.ProtocolMessage.AccessToken }); 
       } 
      } 
     }; 

     app.UseOpenIdConnectAuthentication(openIdConfig); 

     app.UseResourceAuthorization(new AuthorizationManager()); 

     app.Map("/api", inner => 
     { 
      var bearerTokenOptions = new IdentityServerBearerTokenAuthenticationOptions 
      { 
       Authority = "https://localhost:44301/identity", 
       RequiredScopes = new[] { "baseballStatsApi" } 
      }; 

      inner.UseIdentityServerBearerTokenAuthentication(bearerTokenOptions); 
      var config = new HttpConfiguration(); 
      config.MapHttpAttributeRoutes(); 
      inner.UseWebApi(config); 
     });             
    } 
} 

Это решило мою проблему. Теперь я могу получить доступ к страницам MVC с проверкой подлинности на основе файлов cookie и вызвать API с аутентификацией маркера на предъявителя.

 Смежные вопросы

  • Нет связанных вопросов^_^