01-.net core6 获取appsettings.json数据
2022/6/21 23:19:50
本文主要是介绍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数据的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-03-01沐雪多租宝商城源码从.NetCore3.1升级到.Net6的步骤
- 2024-12-06使用Microsoft.Extensions.AI在.NET中生成嵌入向量
- 2024-11-18微软研究:RAG系统的四个层次提升理解与回答能力
- 2024-11-15C#中怎么从PEM格式的证书中提取公钥?-icode9专业技术文章分享
- 2024-11-14云架构设计——如何用diagrams.net绘制专业的AWS架构图?
- 2024-05-08首个适配Visual Studio平台的国产智能编程助手CodeGeeX正式上线!C#程序员必备效率神器!
- 2024-03-30C#设计模式之十六迭代器模式(Iterator Pattern)【行为型】
- 2024-03-29c# datetime tryparse
- 2024-02-21list find index c#
- 2024-01-24convert toint32 c#