带有System.IdentityModel.Tokens.Jwt版本5.1.4的JWT令牌

用户名

我正在使用以下代码生成JWT令牌。

        string audienceId = "099153c2625149bc8ecb3e85e03f0022";
        string secretKey = "IxrAjDoa2FqElO7IhrSrUJELhUckePEPVpaePlS_Xaw";
        var keyByteArray = TextEncodings.Base64Url.Decode(secretKey);

        var issued = data.Properties.IssuedUtc;
        var expires = data.Properties.ExpiresUtc;

        IList<Claim> claimCollection = new List<Claim>
        {
            new Claim(ClaimTypes.Name, "Test")
            , new Claim(ClaimTypes.Country, "Sweden")
            , new Claim(ClaimTypes.Gender, "M")
            , new Claim(ClaimTypes.Surname, "Nemes")
            , new Claim(ClaimTypes.Email, "[email protected]")
            , new Claim(ClaimTypes.Role, "IT")
        };

        var tokenDescriptor = new SecurityTokenDescriptor
        {
            Subject =  new ClaimsIdentity(claimCollection),
            Issuer = _issuer,
            Audience = audienceId,
            Expires = expires.Value.DateTime,
            SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(keyByteArray), SecurityAlgorithms.HmacSha256)
        };
        var tokenHandler = new JwtSecurityTokenHandler();

        var securityToken = tokenHandler.CreateToken(tokenDescriptor);
        return tokenHandler.WriteToken(securityToken);`

如果我在https://jwt.io/中验证了生成的代码,则证明签名无效。

我正在使用以下内容来验证令牌。

        var token = new JwtSecurityToken(model.Token);
        string ClientId = "099153c2625149bc8ecb3e85e03f0022";
        string Base64Secret = "IxrAjDoa2FqElO7IhrSrUJELhUckePEPVpaePlS_Xaw";
        var keyByteArray = TextEncodings.Base64Url.Decode(Base64Secret);

        var validationParameters = new TokenValidationParameters
        {
            IssuerSigningKey = new SymmetricSecurityKey(keyByteArray),
            ValidIssuer = "CBEAE4B7-A490-430A-85C7-865D051C21E6",
            ValidAudience = ClientId
        };

        var tokenHandler = new JwtSecurityTokenHandler();

        SecurityToken validatedToken;
        ClaimsPrincipal claimsPrincipal = tokenHandler.ValidateToken(model.Token, validationParameters, out validatedToken);

我收到无效签名作为例外。最新版本的System.IdentityModel.Tokens.Jwt(版本5.1.4)可用的文档很少。请注意,我也无法降级dll。

我不确定我要去哪里错了。感谢对此的任何帮助。

蓝调

尝试使用Iris在此处建议的其他解码器进行验证

我的场景是我有一个ASP.NET JWT AuthorizationServer,需要通过ASPNET CORE JWT ResourceServer进行身份验证,下面的代码对我有用。

public static class Base64UrlTextEncoder /*: ITextEncoder*/
    {
        public static string Encode(byte[] data)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }

            return Convert.ToBase64String(data).TrimEnd('=').Replace('+', '-').Replace('/', '_');
        }

        public static byte[] Decode(string text)
        {
            if (text == null)
            {
                throw new ArgumentNullException("text");
            }

            return Convert.FromBase64String(Pad(text.Replace('-', '+').Replace('_', '/')));
        }

        private static string Pad(string text)
        {
            var padding = 3 - ((text.Length + 3) % 4);
            if (padding == 0)
            {
                return text;
            }
            return text + new string('=', padding);
        }
    }

用法

var base64key = Base64UrlTextEncoder.Decode("IxrAjDoa2FqElO7IhrSrUJELhUckePEPVpaePlS_Xaw");
var issuerSigningKey = new SymmetricSecurityKey(base64key);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在ASP.NET 5中使用System.IdentityModel.Tokens.Jwt编码JWT令牌

使用System.IdentityModel.Tokens.Jwt解码和验证JWT令牌

使用System.IdentityModel.Tokens.Jwt使用x5c标头参数创建JWT令牌

如何使用System.IdentityModel.Tokens.Jwt包提取令牌到期时间

使用System.IdentityModel.Tokens.Jwt验证C#中生成的令牌

没有发行者的System.Identitymodel.Tokens.jwt可能吗?

System.IdentityModel.Tokens.Jwt的更新导致IdentityServer3 Client中的重大更改

在Azure上部署后无法加载文件或程序集'System.IdentityModel.Tokens.Jwt

名称空间System.IdentityModel.Tokens中不存在“ Jwt”-仅在生产中

.Net 6 的 System.IdentityModel.Tokens.Jwt 的自定义对象 JSON 序列化实现是否存在问题?

如何使用System.IdentityModel.Tokens.Jwt使用Google OAuth2兼容算法RSA SHA-256生成JWT?

如何使用.NET 5的IdentityModel扩展生成JWT令牌

System.IdentityModel.Tokens.JwtSecurityToken定制属性

使用System.IdentityModel.Tokens.Jwt从1.1迁移到2.0后,JWTAuthentication无法在ASP.NET Core 2.0中工作-5.1.4更新

如何在asp.net核心中为JwtBearer和System.IdentityModel.Tokens.Jwt自定义Bearer标头关键字?

System.IdentityModel.Tokens.SessionSecurityTokenHandler和System.IdentityModel.Services.Tokens.MachineKeySessionSecurityTokenHandler DLL在哪里

System.IdentityModel.Tokens和Microsoft.IdentityModel.Tokens之间的冲突

WCF:EncryptedKey子句未使用必需的加密令牌'System.IdentityModel.Tokens.X509SecurityToken'包装

无法解析类型“System.IdentityModel.Tokens.Saml2SecurityTokenHandler, System.IdentityModel”

使用带有IdentityModel 4.1.1的刷新令牌的请求访问令牌

JWT Tokens 结构说明

IDX10501:签名验证失败。尝试使用的密钥:“ System.IdentityModel.Tokens.X509AsymmetricSecurityKey”

带有JWT令牌的Laravel返回的刷新令牌太长

System.IdentityModel.Tokens和Microsoft.IdentityModel.Tokens有什么区别?我应该在ASP.NET Core应用程序中使用哪一个?

无法从“ Microsoft.IdentityModel.Tokens.SymmetricSecurityKey”转换为“ Microsoft.IdentityModel.Tokens.SigningCredentials”

带有JWT Bearer令牌的身份验证Swagger

如何使用angular jwt拦截带有离子存储的令牌?

带有Authlib 0.11的JWT令牌生成器

带有过期JWT刷新令牌的滑动窗口