asp.net-web-api – 如何在ConfigureServices方法中获取IOptions
发布时间:2020-12-16 07:07:10 所属栏目:asp.Net 来源:网络整理
导读:我正在开发asp .net核心web api 2.1应用程序. 我在静态类中添加JWT身份验证服务作为扩展方法: public static class AuthenticationMiddleware { public static IServiceCollection AddJwtAuthentication(this IServiceCollection services,string issuer,st
我正在开发asp .net核心web api 2.1应用程序.
我在静态类中添加JWT身份验证服务作为扩展方法: public static class AuthenticationMiddleware { public static IServiceCollection AddJwtAuthentication(this IServiceCollection services,string issuer,string key) { services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { options.TokenValidationParameters = new TokenValidationParameters { // validate the server that created that token ValidateIssuer = true,// ensure that the recipient of the token is authorized to receive it ValidateAudience = true,// check that the token is not expired and that the signing key of the issuer is valid ValidateLifetime = true,// verify that the key used to sign the incoming token is part of a list of trusted keys ValidateIssuerSigningKey = true,ValidIssuer = issuer,ValidAudience = issuer,IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(key)) }; }); return services; } } 我在Startup类的ConfigureServices方法中使用如下: public void ConfigureServices(IServiceCollection services) { // adding some services here services.AddJwtAuthentication(Configuration["Jwt:Issuer"],Configuration["Jwt:Key"]); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); } 现在,我需要使用IOptions模式从appsettings.json获取JWT身份验证数据 如何在ConfigureServices方法中获取IOptions以将issuer和key传递给extension方法?或者如何将IOptions传递给扩展方法? 解决方法
要将数据从appsettings.json绑定到Model,您可以按照以下步骤操作:
> Appsettings.json内容 { "Logging": { "IncludeScopes": false,"LogLevel": { "Default": "Warning" } },"JWT": { "Issuer": "I","Key": "K" } } > JWT选项 public class JwtOptions { public string Issuer { get; set; } public string Key { get; set; } } > Startup.cs public void ConfigureServices(IServiceCollection services) { services.Configure<JwtOptions>(Configuration.GetSection("JWT")); var serviceProvider = services.BuildServiceProvider(); var opt = serviceProvider.GetRequiredService<IOptions<JwtOptions>>().Value; services.AddJwtAuthentication(opt.Issuer,opt.Key); services.AddMvc(); } >还有一个直接传递JwtOptions的选项. public void ConfigureServices(IServiceCollection services) { services.Configure<JwtOptions>(Configuration.GetSection("JWT")); var serviceProvider = services.BuildServiceProvider(); var opt = serviceProvider.GetRequiredService<IOptions<JwtOptions>>().Value; services.AddJwtAuthentication(opt); services.AddMvc(); } >更改扩展方法. public static IServiceCollection AddJwtAuthentication(this IServiceCollection services,JwtOptions opt) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 编辑器在没有@foreach的情况下不渲染可枚举
- asp.net-core – db ASP.NET Core中的临时保存密码
- asp.net – 从磁条输入信号卡
- asp.net-mvc – ASP.NET MVC – 构建URL或链接的HTML扩展方
- asp.net – 对LOCAL AUTHORITY声明和外部提供商声明的混淆
- asp.net – 在SDL Tridion 2011中将JSON文件上传为多媒体组
- asp.net-mvc – ASP.NET的恶意抓取工具拦截器
- asp.net-mvc – ASP.Net MVC 4通用主要困难
- asp.net-web-api – WebApi2属性路由404
- asp.net – 无法为网站禁用SSL
推荐文章
站长推荐
- asp.net – 为什么ModalPopupExtender不通过java
- kendo-ui – Kendo Grid阻止默认请求
- asp.net-mvc-3 – MVC 3(Razor) – 使用Button事
- asp.net-mvc – Asp mvc 3 noobie:为什么代码优
- asp.net – 从中??等信任环境的Web.config读取sy
- asp.net – 单个应用程序中的多个母版页
- ASP.NET MVC 4自定义HTML Helpers文件夹位置
- asp.net-mvc-3 – 在MVC中设置403错误页面
- asp.net – 替换web.config变换中的IIS重写规则
- asp.net-mvc-2 – 将EDMX文件移动到新项目导致:
热点阅读