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

  • <legend id='93Jy4'><style id='93Jy4'><dir id='93Jy4'><q id='93Jy4'></q></dir></style></legend>
  • <small id='93Jy4'></small><noframes id='93Jy4'>

          <bdo id='93Jy4'></bdo><ul id='93Jy4'></ul>
      1. <tfoot id='93Jy4'></tfoot>

        无法使用 ASP.NET Core 从 JWT 令牌获取声明

        时间:2023-06-01
            <tfoot id='Nzt6t'></tfoot>
            1. <i id='Nzt6t'><tr id='Nzt6t'><dt id='Nzt6t'><q id='Nzt6t'><span id='Nzt6t'><b id='Nzt6t'><form id='Nzt6t'><ins id='Nzt6t'></ins><ul id='Nzt6t'></ul><sub id='Nzt6t'></sub></form><legend id='Nzt6t'></legend><bdo id='Nzt6t'><pre id='Nzt6t'><center id='Nzt6t'></center></pre></bdo></b><th id='Nzt6t'></th></span></q></dt></tr></i><div id='Nzt6t'><tfoot id='Nzt6t'></tfoot><dl id='Nzt6t'><fieldset id='Nzt6t'></fieldset></dl></div>
              <legend id='Nzt6t'><style id='Nzt6t'><dir id='Nzt6t'><q id='Nzt6t'></q></dir></style></legend>
                  <tbody id='Nzt6t'></tbody>
                • <bdo id='Nzt6t'></bdo><ul id='Nzt6t'></ul>

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

                  本文介绍了无法使用 ASP.NET Core 从 JWT 令牌获取声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我正在尝试使用 ASP.NET Core 实现一个非常简单的 JWT 不记名身份验证.我从控制器返回的响应有点像这样:

                  I'm trying to do a really simple implementation of JWT bearer authentication with ASP.NET Core. I return a response from a controller a bit like this:

                      var identity = new ClaimsIdentity();
                      identity.AddClaim(new Claim(ClaimTypes.Name, applicationUser.UserName));
                          var jwt = new JwtSecurityToken(
                               _jwtOptions.Issuer,
                               _jwtOptions.Audience,
                               identity.Claims,
                               _jwtOptions.NotBefore,
                               _jwtOptions.Expiration,
                               _jwtOptions.SigningCredentials);
                  
                         var encodedJwt = new JwtSecurityTokenHandler().WriteToken(jwt);
                  
                         return new JObject(
                             new JProperty("access_token", encodedJwt),
                             new JProperty("token_type", "bearer"),
                             new JProperty("expires_in", (int)_jwtOptions.ValidFor.TotalSeconds),
                             new JProperty(".issued", DateTimeOffset.UtcNow.ToString())
                         );
                  

                  我有用于传入请求的 Jwt 中间件:

                  I have Jwt middleware for incoming requests:

                  app.UseJwtBearerAuthentication(new JwtBearerOptions
                  {
                       AutomaticAuthenticate = true,
                       AutomaticChallenge = true,
                       TokenValidationParameters = tokenValidationParameters
                  });
                  

                  这似乎可以使用 authorize 属性保护资源,但声明从未出现.

                  This seems to work to protect resources with the authorize attribute, but the claims never show up.

                      [Authorize]
                      public async Task<IActionResult> Get()
                      {
                          var user = ClaimsPrincipal.Current.Claims; // Nothing here
                  

                  推荐答案

                  您不能在 ASP.NET Core 应用程序中使用 ClaimsPricipal.Current,因为它不是由运行时设置的.您可以阅读 https://github.com/aspnet/Security/issues/322 了解更多信息.

                  You can't use ClaimsPricipal.Current in an ASP.NET Core application, as it's not set by the runtime. You can read https://github.com/aspnet/Security/issues/322 for more information.

                  相反,请考虑使用 ControllerBase 公开的 User 属性.

                  Instead, consider using the User property, exposed by ControllerBase.

                  这篇关于无法使用 ASP.NET Core 从 JWT 令牌获取声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:OAuth Bearer 令牌身份验证未通过签名验证 下一篇:如何使用 JwtSecurityTokenHandler 和 JWKS 端点验证 JW

                  相关文章

                  最新文章

                  <tfoot id='WMTSr'></tfoot>

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

                    <bdo id='WMTSr'></bdo><ul id='WMTSr'></ul>

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

                    <legend id='WMTSr'><style id='WMTSr'><dir id='WMTSr'><q id='WMTSr'></q></dir></style></legend>