久久久久久久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的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  限時送ChatGPT賬號..

                  我正在開發(fā)一個 ASP Net Core 2.1 Web API.我已經(jīng)在我的項目中成功實現(xiàn)了 JWT.授權(quán)的一切工作正常.

                  通常,當(dāng)我需要用戶聲明時,我知道我可以這樣獲得它們(例如電子郵件聲明):

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

                  問題是,我不在從 ControllerBase 類繼承的控制器中,所以我沒有任何 User 對象或 [Authorize] 屬性.

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

                  <預(yù)> <代碼> eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1bmlxdWVfbmFtZSI6ImFkbWluIiwiZW1haWwiOiJhZG1pbiIsIm5iZiI6MTU2ODYzNjYxMywiZXhwIjoxNTY4NjQ3NDEzLCJpYXQiOjE1Njg2MzY2MTN9.ED9x_AOvkLQqutb09yh3Huyv0ygHp_i3Eli8WG2S9N4

                  我想直接從令牌中獲取聲明,因為:

                  1. 我可以訪問令牌.
                  2. 我不在Controller類中,請求沒有經(jīng)過任何[Authorize]屬性,所以IHttpContextAccessor也不能用.李>

                  如何在 ASP Net Core 2.1 中實現(xiàn)這一點?如果有人想看看我如何添加用戶聲明:

                  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;
                  

                  這篇關(guān)于直接從令牌獲取 JWT 聲明,ASP Net Core 2.1的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

                  相關(guān)文檔推薦

                  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(從函數(shù)調(diào)用按鈕 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>
                            主站蜘蛛池模板: 日韩精品一区二区三区四区视频 | 色www精品视频在线观看 | 国产精品美女在线观看 | 99精品国产一区二区三区 | 亚洲一区二区三区欧美 | 成人精品一区二区三区 | 国产做a爱片久久毛片 | 九九热久久免费视频 | 精品亚洲一区二区 | 一区二区三区国产在线观看 | 国产二区视频 | 欧美视频成人 | h视频免费在线观看 | 欧美极品在线观看 | 日韩视频国产 | 91成人午夜性a一级毛片 | 特级毛片爽www免费版 | 在线不卡av | 国产视频中文字幕在线观看 | 国产成人jvid在线播放 | 国产精品久久久久久久白浊 | 亚洲二区视频 | 亚洲精品视频在线看 | 国产日韩欧美91 | 国产精品成人一区二区三区夜夜夜 | 亚洲成人精品一区 | 日本精品一区二区三区四区 | 亚洲成人网在线 | 黄篇网址| 涩涩视频在线观看 | 亚洲欧美精品在线 | 亚洲午夜视频 | 中文字幕亚洲一区 | 亚洲国产精品日本 | 一区二区三区国产好 | 欧美一区在线视频 | 亚洲国产精品久久人人爱 | 国产精品一区二区三区免费观看 | 黄色片在线看 | 日韩精品一区二区三区四区 | 色播视频在线观看 |