深圳龙华住房和建设局网站官网,网站开发服务费计入哪个科目,网络服务示范区创建情况,公司网站建设策划如果使用 IdentityServer4 做授权服务的负载均衡#xff0c;默认情况下是不可以的#xff0c;比如有两个授权服务站点#xff0c;一个资源服务绑定其中一个授权服务#xff08;Authority配置#xff09;#xff0c;如果通过另外一个授权服务获取access_token#xff0c;…如果使用 IdentityServer4 做授权服务的负载均衡默认情况下是不可以的比如有两个授权服务站点一个资源服务绑定其中一个授权服务Authority配置如果通过另外一个授权服务获取access_token然后拿这个access_token去访问资源服务会报 401 未授权错误为什么原因在这 By default an access token will contain claims about the scope, lifetime (nbf and exp), the client ID (client_id) and the issuer name (iss). 归纳一下生成access_token受影响的因素
scope授权范围服务包含在 scope 内生成的access_token才能访问本服务。lifetime生命周期过期的access_token无效访问。client ID (client_id)不同的客户端 ID生成不同对应的access_token。issuer name (iss)翻译过来“发行者名称”类似于主机名。RSA 加密证书补充不同的加密证书生成不同对应的access_token。
要让负载均衡下的两个授权服务可以正常使用的话需要确保两台授权服务以上五种因素完全一致除了 issuer name (iss)其他因素都是一样的。
IdentityServer4 怎么设置 issuer name (iss)呢答案是通过IssuerUri
IssuerUriSet the issuer name that will appear in the discovery document and the issued JWT tokens. It is recommended to not set this property, which infers the issuer name from the host name that is used by the clients, If not set, the issuer name is inferred from the request.
说明中不建议我们进行设置默认情况下IdentityServer4 会从客户端的主机名中获取可以认为默认情况下issuer nameIssuerUri就是授权服务的主机名比如http://10.9.1.1:5000。
手动设置IssuerUri代码
var builder services.AddIdentityServer(x x.IssuerUri http://111.12.2.21:8000); //slb 地址
资源服务授权配置修改
app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
{Authority http://111.12.2.21:8000, //slb 地址ApiName trade_refund,RequireHttpsMetadata false});
获取access_token示例代码
var client new DiscoveryClient(http://111.12.2.21:8000); //必须是 slb 地址如果是单独的授权服务地址会报错误Value cannot be null. Parameter name: addressclient.Policy.RequireHttps false;var disco await client.GetAsync();var tokenClient new TokenClient(disco.TokenEndpoint, clientId, clientSecret);var tokenResponse tokenClient.RequestClientCredentialsAsync(scope);var accessToken tokenResponse.AccessToken;
通过 HTTP Post 获取access_token不通过 slb直接请求单独的授权服务可以授权访问资源服务获取access_token示例 参考资料
Protecting an API using Client CredentialsIdentityServer Options 相关文章
IdentityServer4(OAuth2.0服务)折腾笔记IdentityServer4 实现 OpenID Connect 和 OAuth 2.0IdentityServer4 使用OpenID Connect添加用户身份验证IdentityServer4 ASP.NET Core的OpenID Connect OAuth 2.0框架学习保护APIIdentityServer4 指定角色授权Authorize(Rolesadmin))IdentityServer4 SigningCredentialRSA 证书加密IdentityServer4 实现自定义 GrantType 授权模式
原文地址http://www.cnblogs.com/xishuai/p/identityserver4-slb.html.NET社区新闻深度好文微信中搜索dotNET跨平台或扫描二维码关注