ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

IdentityServer4 (5) 混合模式(Hybrid)

2020-08-19 08:32:53  阅读:599  来源: 互联网

标签:StandardScopes OidcConstants Hybrid 混合 Scope options IdentityServer4 客户端


写在前面

1、源码(.Net Core 2.2)

  git地址:https://github.com/yizhaoxian/CoreIdentityServer4Demo.git

2、相关章节

  2.1、《IdentityServer4 (1) 客户端授权模式(Client Credentials)
  2.2、《IdentityServer4 (2) 密码授权(Resource Owner Password)
  2.3、《IdentityServer4 (3) 授权码模式(Authorization Code)
  2.4、《IdentityServer4 (4) 静默刷新(Implicit)
  2.5、《IdentityServer4 (5) 混合模式(Hybrid)

3、参考资料

  IdentityServer4 中文文档 http://www.identityserver.com.cn/
  IdentityServer4 英文文档 https://identityserver4.readthedocs.io/en/latest/
  OpenID Connect 官网 https://openid.net/connect/
  OpenID Connect 中文 https://www.cnblogs.com/linianhui/p/openid-connect-core.html
  OpenID Connect和OAuth 2.0对比:https://www.jianshu.com/p/d453076e6433
  Oauth 2.0 官网:https://oauth.net/2/
  Oauth 2.0 授权框架:https://tools.ietf.org/html/rfc6749#section-4.2.1

一、服务端

1、定义客户端

    new Client{
        ClientId="mvc client Hybrid", //客户端Id
        ClientName="测试客户端 Hybrid", //客户端名称 随便写 
        ClientSecrets={ new Secret("mvc secret Hybrid".Sha256()) },

        AllowedGrantTypes=GrantTypes.Hybrid,//验证模式 

        // 如果客户端 response_type 包含 token 这里必须启用
        //AllowAccessTokensViaBrowser=true,

        RedirectUris = { "http://localhost:5003/signin-oidc" },  
        //注销重定向的url
        PostLogoutRedirectUris = { "http://localhost:5003/signout-callback-oidc" },

        AllowOfflineAccess=true,
        AlwaysIncludeUserClaimsInIdToken=true,
          
        //客户端访问权限
        AllowedScopes =
        {
            "api1",
            IdentityServerConstants.StandardScopes.OpenId,
            IdentityServerConstants.StandardScopes.Email,
            IdentityServerConstants.StandardScopes.Address,
            IdentityServerConstants.StandardScopes.Phone,
            IdentityServerConstants.StandardScopes.Profile,
            "roles",
        }

二、客户端

1、修改StartUp.cs

ConfigureServices()

//关闭了 JWT 身份信息类型映射
//这样就允许 well-known 身份信息(比如,“sub” 和 “idp”)无干扰地流过。
//这个身份信息类型映射的“清理”必须在调用 AddAuthentication()之前完成
//区别可参考下面截图,
//简单理解 
//jwt 的 key 映射出来是 http://xxxxxxxxxxxxxxx
//well-known 映射出来是 sub idp 这样简洁的字符
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
//添加认证信息
services.AddAuthentication(options =>
{
    options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
    options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme,
options =>
{
    options.AccessDeniedPath = "/Authorization/NoPermission";
})
.AddOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme, options =>
 {
     //IdentityServer4 服务器地址
     options.Authority = "http://localhost:5002";
     options.ClientId = "mvc client Hybrid";
     options.ClientSecret = "mvc secret Hybrid";
     options.RequireHttpsMetadata = false;
     options.SaveTokens = true;
     options.ResponseType = OidcConstants.ResponseTypes.CodeIdToken;

     //如果请求token 就必须再定义客户端的时候设置运行通过浏览器来返回AccessToken
     //options.ResponseType = OidcConstants.ResponseTypes.CodeToken;
     //options.ResponseType = OidcConstants.ResponseTypes.CodeIdTokenToken;

     options.Scope.Clear();
     options.Scope.Add("api1");
     options.Scope.Add(OidcConstants.StandardScopes.OpenId);
     options.Scope.Add(OidcConstants.StandardScopes.Email);
     options.Scope.Add(OidcConstants.StandardScopes.Phone);
     options.Scope.Add(OidcConstants.StandardScopes.Address);
     options.Scope.Add(OidcConstants.StandardScopes.Profile);
     options.Scope.Add(OidcConstants.StandardScopes.OfflineAccess);
     options.Scope.Add("roles"); 

     //去掉默认过滤的 claim,这样 User.Claims 里就会出现这个 claim
     options.ClaimActions.Remove("nbf");

     //增加过滤的 claim,这样 User.Claims 里就会删除这个 claim
     options.ClaimActions.DeleteClaim("sid");

     options.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters
     {
         //映射 User.Name
         NameClaimType = JwtClaimTypes.Name,
         RoleClaimType = JwtClaimTypes.Role
     };
 });

Configure()

   app.UseStaticFiles();
   //写在 UseMvc() 前面
   app.UseAuthentication();
   app.UseMvcWithDefaultRoute();

三、API资源

  参考之前的文章《IdentityServer4 (1) 客户端授权模式》

四、测试

1、点击受保护的资源

2、跳转到授权服务器并登陆

3、点击同意,自动跳转回原来的页面

 

4、测试访问 api1 资源

  点击上图按钮 测试api1

 

标签:StandardScopes,OidcConstants,Hybrid,混合,Scope,options,IdentityServer4,客户端
来源: https://www.cnblogs.com/Zing/p/13426880.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有