options 依赖注入的方式

 public class JwtSettingsOptions
{ public const string JwtSettings = "JwtSettings"; /// <summary>
/// 发行人
/// </summary>
public string Issuer { get; set; } /// <summary>
/// 订阅者
/// </summary>
public string Audience { get; set; } /// <summary>
/// 加密key
/// </summary>
public string SecurityKey { get; set; } /// <summary>
/// 过期分钟
/// </summary>
public int ExpMinutes { get; set; } }

方法1

services.AddOptions<JwtSettingsOptions>().Bind(Configuration.GetSection(JwtSettingsOptions.JwtSettings));

方法2

var jwtSettingsOptions = new JwtSettingsOptions();
Configuration.GetSection(JwtSettingsOptions.JwtSettings).Bind(positionOptions);

方法3
//使用 ConfigurationBinder.Get 可能比使用 ConfigurationBinder.Bind 更方便。

var jwtSettingsOptions = new JwtSettingsOptions();
jwtSettingsOptions = Configuration.GetSection(JwtSettingsOptions.JwtSettings).Get<JwtSettingsOptions>();

appsettings.json

{
"JwtSettings": {
"SecurityKey": "BB3647441FF3333A4B5D344B4E64A29B53CE525",
"Audience": "autience",
"Issuer": "isuser",
"ExpMinutes": 120
}
}

  

通过构造函数获取实例

private IOptions<JwtSettingsOptions> _options;

public HomeController( IOptions<JwtSettingsOptions> options)
{
  _options = options;

 JwtSettingsOptions  jwtSetting =_options.Value;

}

05-11 20:52