网站视频上传怎么做,能答题做试卷的网站,宁波seo网络推广公司,展览展会策划公司壹时刻保持学习的喜悦可能你咋一看这个标题不知道什么意思#xff0c;其实我也没想好怎么表达#xff0c;因为是一个特别简单的小知识点。先说下为什么突然说到了Ids4#xff1f;这几天大家都知道#xff0c;我在视频《微服务之eShop讲解》#xff0c;目前讲到了购物车微服… 壹时刻保持学习的喜悦可能你咋一看这个标题不知道什么意思其实我也没想好怎么表达因为是一个特别简单的小知识点。先说下为什么突然说到了Ids4这几天大家都知道我在视频《微服务之eShop讲解》目前讲到了购物车微服务部分看到了官方架构中用到了Ids4的认证平台和保护资源Api和我写的认证方案不一样所以我就开始研究了下官方发现了原因所以想主要是一个项目更新的说明流水账同时也是想再一次的强调官网的重要性。再说下今天要说的内容是什么。如果你经常看我的项目或者跟着我的教程肯定都知道我的BCVP项目是集成了ASPNETCORE、VUE、IDS4三方的统一架构设计平台而且之前的有一篇文章我也说到了Ids4官网已经升级到了4.x——《【Ids4实战】最全的 v4 版本升级指南》。更新的内容还是很多的绝大多数的更新还是ids4认证平台的其实在其他的地方也有了些许的变化今天说的就是关于受保护资源服务器的一个小更新关于ProtectingAPIs这一章节的。好啦现在已经说明了来意剩下的就是上代码了下边提到的代码都是关于Blog.Core里的自己可以更新查看。1、之前版本是如何保护Api的在Authentication_Ids4Setup.cs中我定义了一个服务扩展用来添加Ids4的认证服务其中有两个部分第一个部分就是添加认证服务services.AddAuthentication()
第二个部分就是相应的认证方案只不过之前Ids4的3.x系列有两个写法第一个就是基于AspNetCore服务的public class Startup
{public void ConfigureServices(IServiceCollection services){services.AddMvc();services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options {// 认证服务器的根路径options.Authority https://ids.neters.club;// API资源名options.Audience blog.core.api;});}public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory){app.UseAuthentication();app.UseMvc();}
}
还有一种是Ids4认证服务自己提供的一个处理程序方案public class Startup
{public void ConfigureServices(IServiceCollection services){services.AddMvc();services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme).AddIdentityServerAuthentication(options {// identityserver根路径options.Authority https://ids.neters.club;// API资源名options.ApiName blog.core.api;});}public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory){app.UseAuthentication();app.UseMvc();}
}
这两个用哪一个呢官方更建议使用下边这个是他们自己封装的但是其中的核心还是基于微软的JWT类库封装的Ids4自己封装的类库给出的理由是这样的support for both JWTs and reference tokensextensible caching for reference tokensunified configuration modelscope validation我就简单的翻译下使用这种方案可以同时支持JWT和referencetoken的两个方案还针对后者做了缓存scope验证统一配置模型等等。而且我也在Blog.Core使用了该方案书写代码 services.AddAuthentication(o {o.DefaultScheme JwtBearerDefaults.AuthenticationScheme;o.DefaultChallengeScheme nameof(ApiResponseHandler);o.DefaultForbidScheme nameof(ApiResponseHandler);}).AddIdentityServerAuthentication(options {options.Authority Appsettings.app(new string[] { Startup, IdentityServer4, AuthorizationUrl });options.RequireHttpsMetadata false;options.ApiName Appsettings.app(new string[] { Startup, IdentityServer4, ApiName });options.SupportedTokens SupportedTokens.Jwt;options.ApiSecret api_secret;})但是这里有一个情况就是必须添加ids4自己封装的nuget包PackageReference IncludeIdentityServer4.AccessTokenValidation Version3.0.1 /
这个nuget包也在GitHub上开源了后来官方好像也发现了这个情况既然要使用微软的那就用吧不用在api资源服务器里引用了所以我们可以看这个GitHub仓库已经归档了而且在下边的README里也写了启用并停止更新了而且官方的最新文档里也悄悄的发生了变化。2、最新版方案AddJwtBearer从最新的Ids4官方文档https://docs.identityserver.io/en/latest/topics/apis.html中也可以看出来官方也已经取消了上边的那种方案统一使用AddJwtBearer方法了这里有两个好处1、可以和普通的jwt认证统一因为之前是jwt用AddJwtBearerids4用的AddIdentityServerAuthentication。2、可以取消Api资源服务中对Ids4的引入比如那个nuget包。所以最终的代码是这样的 // 添加Identityserver4认证services.AddAuthentication(o {o.DefaultScheme JwtBearerDefaults.AuthenticationScheme;o.DefaultChallengeScheme nameof(ApiResponseHandler);o.DefaultForbidScheme nameof(ApiResponseHandler);}).AddJwtBearer(options {options.Authority Appsettings.app(new string[] { Startup, IdentityServer4, AuthorizationUrl });options.RequireHttpsMetadata false;options.Audience Appsettings.app(new string[] { Startup, IdentityServer4, ApiName });}).AddSchemeAuthenticationSchemeOptions, ApiResponseHandler(nameof(ApiResponseHandler), o { });只不过这里只支持JwtTokens的模式如果想要支持referenceTokens需要先引用包PackageReference IncludeIdentityModel.AspNetCore.OAuth2Introspection Version4.0.1 /
然后services.AddAuthentication(token)// JWT tokens.AddJwtBearer(token, options {options.Authority Constants.Authority;options.Audience resource1;options.TokenValidationParameters.ValidTypes new[] { atjwt };// if token does not contain a dot, it is a reference tokenoptions.ForwardDefaultSelector Selector.ForwardReferenceToken(introspection);})// reference tokens.AddOAuth2Introspection(introspection, options {options.Authority Constants.Authority;options.ClientId resource1;options.ClientSecret secret;});
好啦暂时就这么多吧可能有些凌乱具体查看项目代码吧最后还是那句话官网很重要。 BCVP开发者社区推荐欢迎你来