<legend id='vcLF5'><style id='vcLF5'><dir id='vcLF5'><q id='vcLF5'></q></dir></style></legend>
      1. <tfoot id='vcLF5'></tfoot>
          <bdo id='vcLF5'></bdo><ul id='vcLF5'></ul>

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

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

        直接从令牌获取 JWT 声明,ASP Net Core 2.1

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

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

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

                <tfoot id='WkgX8'></tfoot>

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

                  问题描述

                  限时送ChatGPT账号..

                  我正在开发一个 ASP Net Core 2.1 Web API.我已经在我的项目中成功实现了 JWT.授权的一切工作正常.

                  通常,当我需要用户声明时,我知道我可以这样获得它们(例如电子邮件声明):

                  var claimIdentity = User.Identity as ClaimsIdentity;var emailClaim = claimIdentity.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Email);

                  问题是,我不在从 ControllerBase 类继承的控制器中,所以我没有任何 User 对象或 [Authorize] 属性.

                  我拥有的是令牌本身.
                  例如

                  <预> <代码> eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1bmlxdWVfbmFtZSI6ImFkbWluIiwiZW1haWwiOiJhZG1pbiIsIm5iZiI6MTU2ODYzNjYxMywiZXhwIjoxNTY4NjQ3NDEzLCJpYXQiOjE1Njg2MzY2MTN9.ED9x_AOvkLQqutb09yh3Huyv0ygHp_i3Eli8WG2S9N4

                  我想直接从令牌中获取声明,因为:

                  1. 我可以访问令牌.
                  2. 我不在Controller类中,请求没有经过任何[Authorize]属性,所以IHttpContextAccessor也不能用.李>

                  如何在 ASP Net Core 2.1 中实现这一点?如果有人想看看我如何添加用户声明:

                  var tokenDescriptor = new SecurityTokenDescriptor{过期 = DateTime.UtcNow.AddHours(3),主题 = 新的 ClaimsIdentity(new[]{新声明(ClaimTypes.Name,电子邮件),新声明(ClaimTypes.Email、电子邮件)}),SigningCredentials = new SigningCredentials(密钥:新 SymmetricSecurityKey(密钥),算法:SecurityAlgorithms.HmacSha256Signature)};var token = tokenHandler.CreateToken(tokenDescriptor);

                  我位于一个派生自 IDocumentFilter(Swagger 类)的类中

                  解决方案

                  这是一个简单的解决方法:

                   var tokenDescriptor = new SecurityTokenDescriptor{过期 = DateTime.UtcNow.AddHours(3),主题 = 新的 ClaimsIdentity(new[]{新声明(ClaimTypes.Name,user@hotmail.com"),新声明(ClaimTypes.Email,user@hotmail.com")}),SigningCredentials = new SigningCredentials(密钥:新 SymmetricSecurityKey(密钥),算法:SecurityAlgorithms.HmacSha256Signature)};var Securitytoken = new JwtSecurityTokenHandler().CreateToken(tokenDescriptor);var tokenstring = new JwtSecurityTokenHandler().WriteToken(Securitytoken);var token = new JwtSecurityTokenHandler().ReadJwtToken(tokenstring);var claim = token.Claims.First(c => c.Type == "email").Value;退货索赔;

                  I working on an ASP Net Core 2.1 Web API. I've implemented successfully JWT within my project. Everything with the Authorization works fine.

                  Normally, when I need user claims, I know I can get them like this (E.g. Email claim):

                  var claimsIdentity = User.Identity as ClaimsIdentity;
                  var emailClaim = claimsIdentity.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Email);
                  

                  The thing is, I am not in a controller that inherits from ControllerBase class, so I don't have any User object or [Authorize] attributes.

                  What I have though is the token itself.
                  e.g.

                  eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1bmlxdWVfbmFtZSI6ImFkbWluIiwiZW1haWwiOiJhZG1pbiIsIm5iZiI6MTU2ODYzNjYxMywiZXhwIjoxNTY4NjQ3NDEzLCJpYXQiOjE1Njg2MzY2MTN9.ED9x_AOvkLQqutb09yh3Huyv0ygHp_i3Eli8WG2S9N4
                  

                  I want to get the claims directly from the token, because:

                  1. I have access to the token.
                  2. I am not located in a Controller class and the request is not going through any [Authorize] attributes, so IHttpContextAccessor can't be used as well.

                  How can I achieve this in ASP Net Core 2.1? In case someone wants to see how I add the user claims:

                  var tokenDescriptor = new SecurityTokenDescriptor
                  {
                      Expires = DateTime.UtcNow.AddHours(3),
                      Subject = new ClaimsIdentity(new[]
                      {
                          new Claim(ClaimTypes.Name, email),
                          new Claim(ClaimTypes.Email, email)
                      }),
                      SigningCredentials = new SigningCredentials(key: new SymmetricSecurityKey(key), algorithm: SecurityAlgorithms.HmacSha256Signature)
                  };
                  
                  var token = tokenHandler.CreateToken(tokenDescriptor);
                  

                  I'm located in a class that derives from IDocumentFilter (Swagger class)

                  解决方案

                  Here is a simple workaround:

                      var tokenDescriptor = new SecurityTokenDescriptor
                          {
                              Expires = DateTime.UtcNow.AddHours(3),
                              Subject = new ClaimsIdentity(new[]
                              {
                                  new Claim(ClaimTypes.Name, "user@hotmail.com"),
                                  new Claim(ClaimTypes.Email, "user@hotmail.com")
                              }),
                              SigningCredentials = new SigningCredentials(key: new SymmetricSecurityKey(key), algorithm: SecurityAlgorithms.HmacSha256Signature)
                          };
                  
                      var Securitytoken = new JwtSecurityTokenHandler().CreateToken(tokenDescriptor);
                      var tokenstring = new JwtSecurityTokenHandler().WriteToken(Securitytoken);
                      var token = new JwtSecurityTokenHandler().ReadJwtToken(tokenstring);
                      var claim = token.Claims.First(c => c.Type == "email").Value;
                      return claim;
                  

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

                  上一篇:如何使用 JwtSecurityTokenHandler 和 JWKS 端点验证 JW 下一篇:如何从经过身份验证的 SecurityToken 中获取声明

                  相关文章

                  最新文章

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

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

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

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