• <tfoot id='chZyi'></tfoot>

        <small id='chZyi'></small><noframes id='chZyi'>

          <bdo id='chZyi'></bdo><ul id='chZyi'></ul>
      1. <legend id='chZyi'><style id='chZyi'><dir id='chZyi'><q id='chZyi'></q></dir></style></legend>
        <i id='chZyi'><tr id='chZyi'><dt id='chZyi'><q id='chZyi'><span id='chZyi'><b id='chZyi'><form id='chZyi'><ins id='chZyi'></ins><ul id='chZyi'></ul><sub id='chZyi'></sub></form><legend id='chZyi'></legend><bdo id='chZyi'><pre id='chZyi'><center id='chZyi'></center></pre></bdo></b><th id='chZyi'></th></span></q></dt></tr></i><div id='chZyi'><tfoot id='chZyi'></tfoot><dl id='chZyi'><fieldset id='chZyi'></fieldset></dl></div>
      2. Jwt Bearer 和依赖注入

        时间:2023-06-03

            <tbody id='jeXxh'></tbody>
            <tfoot id='jeXxh'></tfoot>

              1. <legend id='jeXxh'><style id='jeXxh'><dir id='jeXxh'><q id='jeXxh'></q></dir></style></legend>

                • <bdo id='jeXxh'></bdo><ul id='jeXxh'></ul>
                  <i id='jeXxh'><tr id='jeXxh'><dt id='jeXxh'><q id='jeXxh'><span id='jeXxh'><b id='jeXxh'><form id='jeXxh'><ins id='jeXxh'></ins><ul id='jeXxh'></ul><sub id='jeXxh'></sub></form><legend id='jeXxh'></legend><bdo id='jeXxh'><pre id='jeXxh'><center id='jeXxh'></center></pre></bdo></b><th id='jeXxh'></th></span></q></dt></tr></i><div id='jeXxh'><tfoot id='jeXxh'></tfoot><dl id='jeXxh'><fieldset id='jeXxh'></fieldset></dl></div>

                  <small id='jeXxh'></small><noframes id='jeXxh'>

                  本文介绍了Jwt Bearer 和依赖注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我正在尝试配置我的 Jwt Bearer 颁发者密钥,但在生产中,我通常使用由 KeyManager 包装的 Azure Key Vault.KeyManager 类是在依赖注入中配置的,但是在 ConfigureServices 方法中我不能使用它(显然),但是如果我不能使用它,我就无法检索我的密钥.

                  I am trying to configure my Jwt Bearer issuer key but, in production usually, I use Azure Key Vault wrapped by a KeyManager. The KeyManager class is configured in Dependency Injection but, in ConfigureServices method I cannot use that (obviously), but if I cannot use that I cannot retrieve my key.

                  我目前的解决方案是建立一个临时服务提供者并使用它,但我认为不是最先进的(我需要创建两个单例副本,不是最好的).

                  My solution at the moment is to build a temporary service provider and use it, but I think is not the state of the art (and I need to create two copies of singletons, not the best).

                  services.AddAuthentication(options =>
                  {
                      options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                      options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
                      options.DefaultSignInScheme = JwtBearerDefaults.AuthenticationScheme;
                  }).AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, options =>
                  {
                      ServiceProvider sp = services.BuildServiceProvider();
                      IKeyManager keyManager = sp.GetService<KeyManager>();
                  
                      options.TokenValidationParameters = new TokenValidationParameters
                      {
                          ValidateIssuerSigningKey = true,
                          IssuerSigningKey = keyManager.GetSecurityKeyFromName("jwt").Result,
                  
                          ValidIssuer = "https://api.example.com",
                          ValidateIssuer = true
                      };
                  
                      options.Audience = "https://api.example.com";
                      options.Authority = "https://api.example.com";
                  
                      options.SaveToken = true;
                  });
                  

                  推荐答案

                  使用 选项模式并实现IConfigureNamedOptions<JwtBearerOptions>:

                  public class ConfigureJwtBearerOptions : IConfigureNamedOptions<JwtBearerOptions>
                  {
                      private readonly IKeyManager _keyManager;
                  
                      public ConfigureJwtBearerOptions(IKeyManager keyManager)
                      {
                          _keyManager = keyManager;
                      }
                  
                      public void Configure(JwtBearerOptions options)
                      {
                          options.TokenValidationParameters = new TokenValidationParameters
                          {
                              ValidateIssuerSigningKey = true,
                              IssuerSigningKey = _keyManager.GetSecurityKeyFromName("jwt").Result,
                  
                              ValidIssuer = "https://api.example.com",
                              ValidateIssuer = true
                          };
                  
                          options.Audience = "https://api.example.com";
                          options.Authority = "https://api.example.com";
                  
                          options.SaveToken = true;
                      }
                  
                      public void Configure(string name, JwtBearerOptions options)
                      {
                          Configure(options);
                      }
                  }
                  

                  Startup.cs中:

                  services.AddAuthentication(options =>
                  {
                      options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                      options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
                      options.DefaultSignInScheme = JwtBearerDefaults.AuthenticationScheme;
                  }).AddJwtBearer();
                  
                  services.ConfigureOptions<ConfigureJwtBearerOptions>();
                  

                  这篇关于Jwt Bearer 和依赖注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:JWT 令牌错误 401 Unauthorized in .net core 3.1 下一篇:为 Firebase 生成 JWT

                  相关文章

                  最新文章

                  • <bdo id='i4Ezg'></bdo><ul id='i4Ezg'></ul>

                • <legend id='i4Ezg'><style id='i4Ezg'><dir id='i4Ezg'><q id='i4Ezg'></q></dir></style></legend>
                  <i id='i4Ezg'><tr id='i4Ezg'><dt id='i4Ezg'><q id='i4Ezg'><span id='i4Ezg'><b id='i4Ezg'><form id='i4Ezg'><ins id='i4Ezg'></ins><ul id='i4Ezg'></ul><sub id='i4Ezg'></sub></form><legend id='i4Ezg'></legend><bdo id='i4Ezg'><pre id='i4Ezg'><center id='i4Ezg'></center></pre></bdo></b><th id='i4Ezg'></th></span></q></dt></tr></i><div id='i4Ezg'><tfoot id='i4Ezg'></tfoot><dl id='i4Ezg'><fieldset id='i4Ezg'></fieldset></dl></div>
                • <tfoot id='i4Ezg'></tfoot>

                      <small id='i4Ezg'></small><noframes id='i4Ezg'>