【问题标题】:error while generating jwt SignatureAlgorithm is not supported不支持生成 jwt SignatureAlgorithm 时出错
【发布时间】:2016-12-04 01:26:16
【问题描述】:

我想生成 jwt 令牌,该令牌应由 google firebase 验证。下面是我生成 jwt 令牌的代码,它工作正常,直到我将算法更改为“RsaSha256Signature”它然后给我错误

“异常:'System.InvalidOperationException:此上下文不支持加密算法'http://www.w3.org/2001/04/xmldsig-more#rsa-sha256'。 "

如果我不更改它并将其用作“HmacSha256Signature”它可以正常工作

            var plainTextSecurityKey = "-----BEGIN PRIVATE KEY-----;
            var signingKey = new InMemorySymmetricSecurityKey(Encoding.UTF8.GetBytes(plainTextSecurityKey));
            var signingCredentials = new SigningCredentials(signingKey,
                SecurityAlgorithms.HmacSha256Signature, SecurityAlgorithms.Sha256Digest);

            var claimsIdentity = new ClaimsIdentity(new List<Claim>()
        {
            new Claim(ClaimTypes.NameIdentifier, email),
            new Claim(ClaimTypes.Role, role),
        }, "Custom");

            var securityTokenDescriptor = new SecurityTokenDescriptor()
            {
                AppliesToAddress = "https://identitytoolkit.googleapis.com/google.identity.identitytoolkit.v1.IdentityToolkit",
                TokenIssuerName = "serviceemail",
                Subject = claimsIdentity,
                SigningCredentials = signingCredentials,
            };

            var tokenHandler = new JwtSecurityTokenHandler();
            var plainToken = tokenHandler.CreateToken(securityTokenDescriptor);
            var signedAndEncodedToken = tokenHandler.WriteToken(plainToken);

            var tokenValidationParameters = new TokenValidationParameters()
            {
                ValidAudiences = new string[]
            {
                "https://identitytoolkit.googleapis.com/google.identity.identitytoolkit.v1.IdentityToolkit",
                "https://identitytoolkit.googleapis.com/google.identity.identitytoolkit.v1.IdentityToolkit"
            },
                ValidIssuers = new string[]
            {
                "service email",
                "service email"
            },
                IssuerSigningKey = signingKey
            };

            SecurityToken validatedToken;
            tokenHandler.ValidateToken(signedAndEncodedToken,
                tokenValidationParameters, out validatedToken);

            return validatedToken.ToString();

【问题讨论】:

    标签: c# jwt firebase-authentication


    【解决方案1】:

    您的signingKey 不是 RSA 密钥,因此您不能使用 RsaSha256Signature。 HmacSha256Signature 有效,因为您正在使用固定密码创建 HMAC 对称密钥

    var plainTextSecurityKey = "-----BEGIN PRIVATE KEY-----;
    var signingKey = new InMemorySymmetricSecurityKey(Encoding.UTF8.GetBytes(plainTextSecurityKey));
    var signingCredentials = new SigningCredentials(signingKey, SecurityAlgorithms.HmacSha256Signature, SecurityAlgorithms.Sha256Digest);
    

    我不是 C# 方面的专家,但您可能需要 this 之类的东西

     // NOTE: Replace this with your actual RSA public/private keypair!
     var provider = new RSACryptoServiceProvider(2048);
     var parameters = provider.ExportParameters(true);
    
     // Build the credentials used to sign the JWT
     var signingKey = new RsaSecurityKey(parameters);
     var signingCredentials = new SigningCredentials(signingKey, SecurityAlgorithms.RsaSha256);
    

    您将需要一个包含您的私钥和公钥的密钥库。注意HMAC是对称算法,签名和验证的key是一样的,但是RSA需要keypair

    【讨论】:

      猜你喜欢
      • 2018-02-22
      • 2017-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-08
      • 1970-01-01
      • 2013-07-20
      • 2019-09-10
      相关资源
      最近更新 更多