1.在appsettings.json中添加数据
1 { 2 "Logging": { 3 "LogLevel": { 4 "Default": "Information", 5 "Microsoft.AspNetCore": "Warning" 6 } 7 }, 8 "AllowedHosts": "*", 9 "JwtSettings": { 10 "Audience": "TZ.NET", 11 "Issuer": "TZ.NET", 12 "SecretKey": "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDI2a2EJ7m872v0afyoSDJT2o1", 13 "Expire": 30 14 } 15 }
2.添加OptionsSetting类
1 /// <summary> 2 /// 获取配置文件POCO实体类 3 /// </summary> 4 public class OptionsSetting 5 { 6 public static string ConnAdmin = "conn_zrAdmin"; 7 8 public JwtSettings JwtSettings { get; set; } 9 } 10 11 /// <summary> 12 /// Jwt 13 /// </summary> 14 public class JwtSettings 15 { 16 /// <summary> 17 /// token是谁颁发的 18 /// </summary> 19 public string Issuer { get; set; } 20 /// <summary> 21 /// token可以给那些客户端使用 22 /// </summary> 23 public string Audience { get; set; } 24 /// <summary> 25 /// 加密的key(SecretKey必须大于16个,是大于,不是大于等于) 26 /// </summary> 27 public string SecretKey { get; set; } 28 /// <summary> 29 /// token时间(分) 30 /// </summary> 31 public int Expire { get; set; } = 1440; 32 }
3.Program.cs 中 Services注册
1 builder.Services.Configure<OptionsSetting>(builder.Configuration);
4.调用时Option注入 调用
1 public class UserController : BaseController 2 { 3 #region Option注入 4 private readonly OptionsSetting optionsSetting; 5 public UserController(IOptions<OptionsSetting> optionsSetting) 6 { 7 this.optionsSetting = optionsSetting.Value; 8 } 9 #endregion 10 11 [HttpGet] 12 public IActionResult Login() 13 { 14 var jwtSettings = optionsSetting.JwtSettings; 15 } 16 }
搜索
复制
原创文章,作者:,如若转载,请注明出处:https://blog.ytso.com/269470.html