久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

  • <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 class="qwawimqqmiuu" id='WkgX8'><tfoot id='WkgX8'></tfoot><dl id='WkgX8'><fieldset id='WkgX8'></fieldset></dl></div>
    1. <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

        Get JWT claims directly from the token, ASP Net Core 2.1(直接從令牌獲取 JWT 聲明,ASP Net Core 2.1)

                <legend id='vcLF5'><style id='vcLF5'><dir id='vcLF5'><q id='vcLF5'></q></dir></style></legend>
              1. <tfoot id='vcLF5'></tfoot>
                  <tbody id='vcLF5'></tbody>
                  <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 class="qwawimqqmiuu" id='vcLF5'><tfoot id='vcLF5'></tfoot><dl id='vcLF5'><fieldset id='vcLF5'></fieldset></dl></div>

                  本文介紹了直接從令牌獲取 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模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  What are good algorithms for vehicle license plate detection?(車牌檢測有哪些好的算法?)
                  onClick event for Image in Unity(Unity中圖像的onClick事件)
                  Running Total C#(運行總 C#)
                  Deleting a directory when clicked on a hyperlink with JAvascript.ASP.NET C#(單擊帶有 JAvascript.ASP.NET C# 的超鏈接時刪除目錄)
                  asp.net listview highlight row on click(asp.net listview 在單擊時突出顯示行)
                  Calling A Button OnClick from a function(從函數調用按鈕 OnClick)
                  <legend id='nvrqI'><style id='nvrqI'><dir id='nvrqI'><q id='nvrqI'></q></dir></style></legend>

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

                        <tbody id='nvrqI'></tbody>

                        • <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 class="qwawimqqmiuu" id='nvrqI'><tfoot id='nvrqI'></tfoot><dl id='nvrqI'><fieldset id='nvrqI'></fieldset></dl></div>
                            主站蜘蛛池模板: 亚洲一二三区不卡 | 欧美一区永久视频免费观看 | 成人一区二区在线 | 亚洲一级二级三级 | 国产精品久久久久久久久久免费看 | 91 视频网站 | 欧美日韩国产精品激情在线播放 | 国产欧美一区二区三区国产幕精品 | 亚洲精品久久视频 | 黄视频欧美 | 人人亚洲| 国产亚洲一区二区精品 | 91精品国产日韩91久久久久久 | 国产激情 | 无码日韩精品一区二区免费 | 亚洲欧美一区二区三区在线 | 国产网站久久 | 国产1区2区在线观看 | 欧美日韩黄| 一区二区三区精品 | 成人网av | 日韩成人一区二区 | 日韩精品在线看 | 4h影视 | 国产精品成人久久久久 | 99re热这里只有精品视频 | 伊人色综合久久久天天蜜桃 | eeuss国产一区二区三区四区 | 久久精品综合网 | 亚洲 欧美 精品 | 最新免费黄色网址 | 久久国产精品网站 | 在线观看成人 | 日韩在线中文 | 成人一区二区三区在线 | 久www| 欧美日韩大片 | av毛片| 99re热这里只有精品视频 | 国产精品99久久久久久www | 久久视频精品 |