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

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

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

      <tfoot id='pGpBI'></tfoot>

    2. <legend id='pGpBI'><style id='pGpBI'><dir id='pGpBI'><q id='pGpBI'></q></dir></style></legend>
      1. 如何使用 System.IdentityModel.Tokens.Jwt 生成具有 Goo

        How to produce JWT with Google OAuth2 compatible algorithm RSA SHA-256 using System.IdentityModel.Tokens.Jwt?(如何使用 System.IdentityModel.Tokens.Jwt 生成具有 Google OAuth2 兼容算法 RSA SHA-256 的 JWT?) - IT屋-程序員軟件開

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

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

              <legend id='zbKic'><style id='zbKic'><dir id='zbKic'><q id='zbKic'></q></dir></style></legend>
                <tbody id='zbKic'></tbody>
                <tfoot id='zbKic'></tfoot>

                  <i id='zbKic'><tr id='zbKic'><dt id='zbKic'><q id='zbKic'><span id='zbKic'><b id='zbKic'><form id='zbKic'><ins id='zbKic'></ins><ul id='zbKic'></ul><sub id='zbKic'></sub></form><legend id='zbKic'></legend><bdo id='zbKic'><pre id='zbKic'><center id='zbKic'></center></pre></bdo></b><th id='zbKic'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='zbKic'><tfoot id='zbKic'></tfoot><dl id='zbKic'><fieldset id='zbKic'></fieldset></dl></div>
                  本文介紹了如何使用 System.IdentityModel.Tokens.Jwt 生成具有 Google OAuth2 兼容算法 RSA SHA-256 的 JWT?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  限時送ChatGPT賬號..

                  我正在嘗試創建一個 JWT 以使用 Google 文檔中所述的服務帳戶進行授權,使用 System.IdentityModel.Tokens.Jwt.我有以下代碼:

                  I'm trying to create a JWT to authorize with a service account as described in Google documentation using System.IdentityModel.Tokens.Jwt. I have the following code:

                  byte[] key = Convert.FromBase64String("...");
                  var certificate = new X509Certificate2(key, "notasecret");
                  
                  DateTime now = DateTime.UtcNow;
                  TimeSpan span = now - UnixEpoch;
                  Claim[] claims =
                  {
                      new Claim("iss", "email@developer.gserviceaccount.com"),
                      new Claim("scope", "https://www.googleapis.com/auth/plus.me"),
                      new Claim("aud", "https://accounts.google.com/o/oauth2/token"),
                      new Claim("iat", span.TotalSeconds.ToString()),
                      new Claim("exp", span.Add(TimeSpan.FromHours(1)).TotalSeconds.ToString())
                  };
                  
                  JwtSecurityTokenHandler handler = new JwtSecurityTokenHandler();
                  var descriptor = new SecurityTokenDescriptor
                  {
                      SigningCredentials = new SigningCredentials(
                          new InMemorySymmetricSecurityKey(key),
                          "http://www.w3.org/2001/04/xmldsig-more#hmac-sha256",
                          "http://www.w3.org/2001/04/xmlenc#sha256"),
                      Subject = new ClaimsIdentity(claims)
                  };
                  
                  JwtSecurityToken jwtSecurityToken = (JwtSecurityToken)handler.CreateToken(descriptor);
                  string json = handler.WriteToken(jwtSecurityToken);
                  

                  哪個輸出:

                  { "typ" : "JWT" , "alg" : "HS256" }
                  

                  雖然 Google 明確聲明它支持 SHA-256:

                  While Google explicitly states it supports SHA-256:

                  服務帳號依賴于 RSA SHA-256 算法和 JWT 令牌格式

                  Service accounts rely on the RSA SHA-256 algorithm and the JWT token format

                  根據 wtSecurityTokenHandler.InboundAlgorithmMap:

                  RS256 => http://www.w3.org/2001/04/xmldsig-more#rsa-sha256
                  HS256 => http://www.w3.org/2001/04/xmldsig-more#hmac-sha256 
                  

                  所以當我更改代碼時:

                  new SigningCredentials(
                      new InMemorySymmetricSecurityKey(key),
                          "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256",
                          "http://www.w3.org/2001/04/xmlenc#sha256");
                  

                  我遇到了一個異常:

                  System.InvalidOperationException: IDX10632: SymmetricSecurityKey.GetKeyedHashAlgorithm( 'http://www.w3.org/2001/04/xmldsig-more#rsa-sha256' ) threw an exception.
                  SymmetricSecurityKey: 'System.IdentityModel.Tokens.InMemorySymmetricSecurityKey'
                  SignatureAlgorithm: 'http://www.w3.org/2001/04/xmldsig-more#rsa-sha256', check to make sure the SignatureAlgorithm is supported.
                  

                  這是否意味著微軟不支持谷歌獨家支持的算法?

                  Does it mean Microsoft doesn't support the algorithm Google supports exclusively?

                  推薦答案

                  private static async Task<string> GetAuthorizationToken(GoogleAuthOptions authOptions)
                  {
                      string jwt = CreateJwt(authOptions);
                  
                      var dic = new Dictionary<string, string>
                      {
                          { "grant_type", "urn:ietf:params:oauth:grant-type:jwt-bearer" },
                          { "assertion", jwt }
                      };
                      var content = new FormUrlEncodedContent(dic);
                  
                      var httpClient = new HttpClient { BaseAddress = new Uri("https://accounts.google.com") };
                      var response = await httpClient.PostAsync("/o/oauth2/token", content);
                      response.EnsureSuccessStatusCode();
                  
                      dynamic dyn = await response.Content.ReadAsAsync<dynamic>();
                      return dyn.access_token;
                  }
                  
                  private static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
                  
                  private static string CreateJwt(GoogleAuthOptions authOptions)
                  {
                      var certificate = new X509Certificate2(Convert.FromBase64String(authOptions.CertificateKey), authOptions.CertificateSecret);
                  
                      DateTime now = DateTime.UtcNow;
                      var claimset = new
                      {
                          iss = authOptions.Issuer,
                          scope = "https://www.googleapis.com/auth/plus.me",
                          aud = authOptions.Audience,
                          iat = ((int)now.Subtract(UnixEpoch).TotalSeconds).ToString(CultureInfo.InvariantCulture),
                          exp = ((int)now.AddMinutes(55).Subtract(UnixEpoch).TotalSeconds).ToString(CultureInfo.InvariantCulture)
                      };
                  
                      // header
                      var header = new { typ = "JWT", alg = "RS256" };
                  
                      // encoded header
                      var headerSerialized = JsonConvert.SerializeObject(header);
                      var headerBytes = Encoding.UTF8.GetBytes(headerSerialized);
                      var headerEncoded = TextEncodings.Base64Url.Encode(headerBytes);
                  
                      // encoded claimset
                      var claimsetSerialized = JsonConvert.SerializeObject(claimset);
                      var claimsetBytes = Encoding.UTF8.GetBytes(claimsetSerialized);
                      var claimsetEncoded = TextEncodings.Base64Url.Encode(claimsetBytes);
                  
                      // input
                      var input = String.Join(".", headerEncoded, claimsetEncoded);
                      var inputBytes = Encoding.UTF8.GetBytes(input);
                  
                      // signiture
                      var rsa = (RSACryptoServiceProvider)certificate.PrivateKey;
                      var cspParam = new CspParameters
                      {
                          KeyContainerName = rsa.CspKeyContainerInfo.KeyContainerName,
                          KeyNumber = rsa.CspKeyContainerInfo.KeyNumber == KeyNumber.Exchange ? 1 : 2
                      };
                      var cryptoServiceProvider = new RSACryptoServiceProvider(cspParam) { PersistKeyInCsp = false };
                      var signatureBytes = cryptoServiceProvider.SignData(inputBytes, "SHA256");
                      var signatureEncoded = TextEncodings.Base64Url.Encode(signatureBytes);
                  
                      // jwt
                      return String.Join(".", headerEncoded, claimsetEncoded, signatureEncoded);
                  }
                  

                  這篇關于如何使用 System.IdentityModel.Tokens.Jwt 生成具有 Google OAuth2 兼容算法 RSA SHA-256 的 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='T7zwt'><style id='T7zwt'><dir id='T7zwt'><q id='T7zwt'></q></dir></style></legend>

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

                          1. <tfoot id='T7zwt'></tfoot>
                            主站蜘蛛池模板: a级毛片国产 | 在线国产中文字幕 | www亚洲精品 | 欧美一区二区免费 | 亚洲一区二区免费视频 | 国产伦精品一区二区三区视频金莲 | 国产精品视频不卡 | a在线观看 | 91看片视频 | 国产成人一区在线 | 日本高清视频在线播放 | 精品蜜桃一区二区三区 | 高清视频一区二区三区 | 不卡一区 | 日韩av免费在线电影 | 欧美日韩亚洲国产 | 成人国产一区二区三区精品麻豆 | 久草在线在线精品观看 | 鸡毛片| 色综合天天天天做夜夜夜夜做 | 久久一区二区三区四区 | 日韩欧美一区二区三区在线播放 | 色毛片 | av福利网 | 午夜一区二区三区 | 成人久久久| 亚洲精品在线免费 | 欧美一区二区久久 | 一色一黄视频 | 高清18麻豆 | 中文字幕日韩欧美 | 伊人久久综合 | 欧美极品一区二区 | 五月综合色啪 | www.玖玖玖 | 不卡av在线 | 中文字幕人成乱码在线观看 | 亚洲欧美视频一区 | 成人精品一区二区三区 | 在线看黄免费 | 成人在线精品视频 |