IdentityServer4 (5) 混合模式(Hybrid)
写在前面
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", //客户端IdClientName="测试客户端 Hybrid", //客户端名称 随便写 ClientSecrets={ new Secret("mvc secret Hybrid".Sha256()) },
AllowedGrantTypes=GrantTypes.Hybrid,//验证模式 // 如果客户端 response_type 包含 token 这里必须启用//AllowAccessTokensViaBrowser=true,RedirectUris = { "http://localhost:5003/signin-oidc" }, //注销重定向的urlPostLogoutRedirectUris = { "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

