Net Core教程

01-.net core6 获取appsettings.json数据

本文主要是介绍01-.net core6 获取appsettings.json数据,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

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 }

 

 

 

 

 

 

 

 

搜索

复制

这篇关于01-.net core6 获取appsettings.json数据的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!