[英].NET Core - JWT - SecurityTokenNoExpirationException
[英]Creating JWT to match the JWT generated by .Net core
与我合作的第三方 API 要求我在客户端生成 JWT。 我了解到他们使用以下代码来验证 JWT。
using System;
using System.Text;
using Microsoft.IdentityModel.Tokens;
using Microsoft.AspNetCore.Builder;
// The key length needs to be of sufficient length, or otherwise an error will occur.
var tokenSecretKey = Encoding.UTF8.GetBytes(Configuration["TokenSecretKey"]);
var tokenValidationParameters = new TokenValidationParameters
{
// Token signature will be verified using a private key.
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(tokenSecretKey),
ValidateIssuer = false,
ValidateAudience = false
};
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(options => { options.RequireHttpsMetaData = false;
options.SaveToken = true;
options.TokenValidationParameters = tokenValidationParameters;
});
在客户端,我使用 Jose JWT 创建的令牌生成如下。只有这似乎适用于我正在使用的 .Net 4.0 框架。
return Jose.JWT.Encode(claims, byteArrayOfKey, Jose.JwsAlgorithm.HS256);
但验证失败,服务器端出现 401。 有什么我可以做的匹配服务器端。
检查客户端和服务器使用的算法是否相同。 基本上,不要忽视发行者,任何人都可以伪造服务器,因此最佳实践是使用发行者作为强制验证。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.