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

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

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

      1. <small id='G0dkB'></small><noframes id='G0dkB'>

        JWT 如何添加自定義聲明和解碼聲明

        JWT How to add custom claims and decode claims(JWT 如何添加自定義聲明和解碼聲明)
      2. <i id='vvSb8'><tr id='vvSb8'><dt id='vvSb8'><q id='vvSb8'><span id='vvSb8'><b id='vvSb8'><form id='vvSb8'><ins id='vvSb8'></ins><ul id='vvSb8'></ul><sub id='vvSb8'></sub></form><legend id='vvSb8'></legend><bdo id='vvSb8'><pre id='vvSb8'><center id='vvSb8'></center></pre></bdo></b><th id='vvSb8'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='vvSb8'><tfoot id='vvSb8'></tfoot><dl id='vvSb8'><fieldset id='vvSb8'></fieldset></dl></div>

            <tfoot id='vvSb8'></tfoot>
              <bdo id='vvSb8'></bdo><ul id='vvSb8'></ul>
                <legend id='vvSb8'><style id='vvSb8'><dir id='vvSb8'><q id='vvSb8'></q></dir></style></legend>

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

                    <tbody id='vvSb8'></tbody>
                  本文介紹了JWT 如何添加自定義聲明和解碼聲明的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  限時送ChatGPT賬號..

                  我正在嘗試檢索我在創建令牌時所做的一些自定義聲明.但是,我不確定我應該寫什么來檢索這些聲明.

                  I am trying to retrieve some custom claims that I made when I created my token. However, I am not sure on what I should write to retrieve those claims.

                  這是我的令牌創建函數

                  public String createToken(AuthenticationDTO Input)
                  {
                      //Set issued at date
                      DateTime issuedAt = DateTime.UtcNow;
                      //set the time when it expires
                      DateTime expires = DateTime.UtcNow.AddDays(7);
                  
                      //http://stackoverflow.com/questions/18223868/how-to-encrypt-jwt-security-token
                      var tokenHandler = new JwtSecurityTokenHandler();
                  
                      //create a identity and add claims to the user which we want to log in
                      ClaimsIdentity claimsIdentity = new ClaimsIdentity(new[]
                      {
                          new Claim("UserName", Input.UserName),
                          new Claim("Email",Input.Email),
                          new Claim("PhoneNumber",Input.PhoneNumber),
                          new Claim("FirstName",Input.FirstName),
                          new Claim("LastName",Input.LastName),
                          new Claim("Id",Input.Id)
                      });
                  
                      const string sec = HostConfig.SecurityKey;
                      var now = DateTime.UtcNow;
                      var securityKey = new SymmetricSecurityKey(System.Text.Encoding.Default.GetBytes(sec));
                      var signingCredentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256Signature);
                  
                  
                      //create the jwt
                      var token =(JwtSecurityToken)
                              tokenHandler.CreateJwtSecurityToken(issuer: HostConfig.Issuer, audience: HostConfig.Audience,
                                  subject: claimsIdentity, notBefore: issuedAt, expires: expires, signingCredentials: signingCredentials);
                      var tokenString = tokenHandler.WriteToken(token);
                  
                      return tokenString;
                  }
                  

                  我決定命名我自己的聲明,而不是使用提供的標準聲明.但是,我不知道如何檢索它們.這是我目前擁有的:

                  Instead of using the standard ones that are provided, I decided to name my own claims. However, I do not know how to retrieve them. This is what I have currently:

                  public AuthenticationDTO DecodeToken(String Input)
                  {
                      var key = Encoding.ASCII.GetBytes(HostConfig.SecurityKey);
                      var handler = new JwtSecurityTokenHandler();
                      var tokenSecure = handler.ReadToken(Input) as SecurityToken;
                      var validations = new TokenValidationParameters
                      {
                          ValidateIssuerSigningKey = true,
                          IssuerSigningKey = new SymmetricSecurityKey(key),
                          ValidateIssuer = false,
                          ValidateAudience = false
                      };
                      var claims = handler.ValidateToken(Input, validations, out tokenSecure);
                      return null;
                  }
                  

                  我注意到我的索賠是這樣來的

                  I noticed that my claims are coming in like this

                  如何提取它們?

                  添加了 AuthentcationDTO

                  Added AuthentcationDTO

                  public class AuthenticationDTO
                  {
                      public String Id { get; set; }
                      public String UserName { get; set; }
                      public String Email { get; set; }
                      public String FirstName { get; set; }
                      public String LastName { get; set; }
                      public String PhoneNumber { get; set; }
                  }
                  

                  推薦答案

                  如果你想獲取聲明,即 preferred_username,你可以從 ClaimsPrincipal 獲取.

                  If you want to gets claims i.e, preferred_username you can get that from ClaimsPrincipal.

                  var user = User as ClaimsPrincipal;
                  string username = user.Claims.Where(c => c.Type == "preferred_username")
                      .Select(x => x.Value).FirstOrDefault();
                  

                  User 將來自 Claims.對于那個寫

                  User will come from Claims. For that write

                  使用 System.Security.Claims;

                  似乎 User 并非在所有版本中都可用.獲得索賠的另一種方法是類似的.

                  It seems that User is not available in all versions. Another way to get claims will be something similar.

                  var prinicpal = (ClaimsPrincipal)Thread.CurrentPrincipal;
                  var email = prinicpal.Claims.Where(c => c.Type == ClaimTypes.Email)
                      .Select(c => c.Value).SingleOrDefault();
                  

                  AuthenticationDTO 分配所有值.

                  public AuthenticationDTO DecodeToken(String Input)
                  {
                      var key = Encoding.ASCII.GetBytes(HostConfig.SecurityKey);
                      var handler = new JwtSecurityTokenHandler();
                      var tokenSecure = handler.ReadToken(Input) as SecurityToken;
                      var validations = new TokenValidationParameters
                      {
                          ValidateIssuerSigningKey = true,
                          IssuerSigningKey = new SymmetricSecurityKey(key),
                          ValidateIssuer = false,
                          ValidateAudience = false
                      };
                      var claims = handler.ValidateToken(Input, validations, out tokenSecure);
                      var prinicpal = (ClaimsPrincipal)Thread.CurrentPrincipal;
                      if (principal is ClaimsPrincipal claims)
                      {
                           return new ApplicationDTO
                               {
                                   Id = claims.Claims.FirstOrDefault(x => x.Type == "sub")?.Value ?? "",
                                   UserName = claims.Claims.FirstOrDefault(x => x.Type == "preferred_username")?.Value ?? "",
                                   Email = claims.Claims.FirstOrDefault(x => x.Type == "email")?.Value ?? ""
                               };
                      }
                      return null;
                  }
                  

                  這篇關于JWT 如何添加自定義聲明和解碼聲明的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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='udeeE'><style id='udeeE'><dir id='udeeE'><q id='udeeE'></q></dir></style></legend>
                        <tfoot id='udeeE'></tfoot>
                          <tbody id='udeeE'></tbody>

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

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

                            <i id='udeeE'><tr id='udeeE'><dt id='udeeE'><q id='udeeE'><span id='udeeE'><b id='udeeE'><form id='udeeE'><ins id='udeeE'></ins><ul id='udeeE'></ul><sub id='udeeE'></sub></form><legend id='udeeE'></legend><bdo id='udeeE'><pre id='udeeE'><center id='udeeE'></center></pre></bdo></b><th id='udeeE'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='udeeE'><tfoot id='udeeE'></tfoot><dl id='udeeE'><fieldset id='udeeE'></fieldset></dl></div>
                          • 主站蜘蛛池模板: 成人h免费观看视频 | 成人av一区二区三区 | 99九色 | 国产在线拍偷自揄拍视频 | 日韩欧美中文 | 一区二区免费 | 中文在线a在线 | 欧美在线视频一区 | 久久国产精品久久久久久 | 国产电影一区二区三区爱妃记 | 国产精品1区 | 超碰在线人人 | 免费一区二区在线观看 | 欧美成人精品激情在线观看 | a国产一区二区免费入口 | 在线成人免费视频 | 欧美乱大交xxxxx另类电影 | 亚洲成人av | 国产精品人人做人人爽 | 99日韩 | 欧美精品欧美精品系列 | 中文在线亚洲 | 久久久日韩精品一区二区三区 | 中文字幕在线精品 | 亚洲精品久久久一区二区三区 | 伊人免费在线 | 久久69精品久久久久久久电影好 | 成人免费视屏 | 青娱乐av | 18av在线播放 | 日韩www| 欧美日韩国产中文字幕 | 91麻豆精品国产91久久久更新资源速度超快 | 国产欧美精品在线 | 国内精品视频在线 | 久久天堂| www一级片 | 色精品视频 | 欧美电影在线 | 午夜黄色 | 精品美女久久久 |