在同一应用程序项目上对API使用承载令牌认证,对MVC使用OpenId认证

蝗虫5304

我正在尝试通过Identity Server在我的应用程序上同时使用OpenId和Bearer令牌认证。

当前的问题是,一旦我对用户进行了身份验证,我仍然需要获取承载令牌,以便能够为我的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网址,并使用openID auth进行其他操作。有没有办法做到这一点?

蝗虫5304

好的,我在以下帖子中找到了一些信息

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

可以在此处找到实现链接中讨论的概念的github存储库

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

基本上,您需要使用app.Map()将api网址映射到其他配置。就我而言,我将启动文件更改为如下所示。

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

那解决了我的问题。现在,我可以使用基于cookie的身份验证访问MVC页面,并使用承载令牌身份验证来调用API。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

同一应用程序上的AAD OpenId和承载身份验证

来自分类Dev

OWIN多应用程序承载令牌认证

来自分类Dev

仅使用令牌的OpenId Connect重新认证

来自分类Dev

使用键盘快捷键在同一应用程序的Windows上循环-GNOME 3

来自分类Dev

在同一应用程序上使用两个Facebook应用程序ID进行测试

来自分类Dev

在同一应用程序上使用两个Facebook应用程序ID进行测试

来自分类Dev

如何在同一应用程序中访问Dropbox SDK和Facebook API的令牌

来自分类Dev

对AJAX Web应用程序使用REST令牌认证的好主意?

来自分类Dev

OWIN承载令牌认证

来自分类Dev

API 管理和应用程序认证

来自分类Dev

在UWP和WP8.1上使用同一应用程序的Facebook登录名-哪个SID?

来自分类Dev

Facebook GET朋友API调用不返回除使用同一应用程序的朋友以外的任何朋友

来自分类Dev

使用AWS Cognito的Java API认证/ autorization春天启动的Web应用程序

来自分类Dev

如何让同一应用程序的多个版本使用相同版本的程序集?C#

来自分类Dev

使用AspNetCoreModuleV2使用同一应用程序池托管多个.net核心

来自分类Dev

Web应用程序中令牌认证的最佳做法?

来自分类Dev

使用不同的日志文件登录同一应用程序

来自分类Dev

Rails在同一应用程序中使用RABL和jbuilder?

来自分类Dev

在同一应用程序中在Google Maps旁边使用Amazon Maps

来自分类Dev

使用.NET多线程运行同一应用程序的多个实例

来自分类Dev

无需团队帐户即可在Xcode中使用同一应用程序

来自分类Dev

如何在git中同时使用同一应用程序的多个版本?

来自分类Dev

如何使用python关闭同一应用程序的两个窗口?

来自分类Dev

如何使用Alt +`在同一应用程序的窗口之间切换

来自分类Dev

在同一应用程序中使用JSP和PHP

来自分类Dev

Android:使用不同的图标创建同一应用程序的不同APK

来自分类Dev

使用.NET多线程运行同一应用程序的多个实例

来自分类Dev

使用同一应用程序中的其他Oscar模型替代Oscar模型

来自分类Dev

在同一应用程序中使用ViewController的副本并具有其他功能

Related 相关文章

  1. 1

    同一应用程序上的AAD OpenId和承载身份验证

  2. 2

    OWIN多应用程序承载令牌认证

  3. 3

    仅使用令牌的OpenId Connect重新认证

  4. 4

    使用键盘快捷键在同一应用程序的Windows上循环-GNOME 3

  5. 5

    在同一应用程序上使用两个Facebook应用程序ID进行测试

  6. 6

    在同一应用程序上使用两个Facebook应用程序ID进行测试

  7. 7

    如何在同一应用程序中访问Dropbox SDK和Facebook API的令牌

  8. 8

    对AJAX Web应用程序使用REST令牌认证的好主意?

  9. 9

    OWIN承载令牌认证

  10. 10

    API 管理和应用程序认证

  11. 11

    在UWP和WP8.1上使用同一应用程序的Facebook登录名-哪个SID?

  12. 12

    Facebook GET朋友API调用不返回除使用同一应用程序的朋友以外的任何朋友

  13. 13

    使用AWS Cognito的Java API认证/ autorization春天启动的Web应用程序

  14. 14

    如何让同一应用程序的多个版本使用相同版本的程序集?C#

  15. 15

    使用AspNetCoreModuleV2使用同一应用程序池托管多个.net核心

  16. 16

    Web应用程序中令牌认证的最佳做法?

  17. 17

    使用不同的日志文件登录同一应用程序

  18. 18

    Rails在同一应用程序中使用RABL和jbuilder?

  19. 19

    在同一应用程序中在Google Maps旁边使用Amazon Maps

  20. 20

    使用.NET多线程运行同一应用程序的多个实例

  21. 21

    无需团队帐户即可在Xcode中使用同一应用程序

  22. 22

    如何在git中同时使用同一应用程序的多个版本?

  23. 23

    如何使用python关闭同一应用程序的两个窗口?

  24. 24

    如何使用Alt +`在同一应用程序的窗口之间切换

  25. 25

    在同一应用程序中使用JSP和PHP

  26. 26

    Android:使用不同的图标创建同一应用程序的不同APK

  27. 27

    使用.NET多线程运行同一应用程序的多个实例

  28. 28

    使用同一应用程序中的其他Oscar模型替代Oscar模型

  29. 29

    在同一应用程序中使用ViewController的副本并具有其他功能

热门标签

归档