JWT无法将标头解码为Base64Url编码的字符串
我有以下代码:
public async Task<LoginResult> GenerateJwtTokenAsync(string email, string password)
{
LoginResult loginResult = await _membershipProvider.Login(email, password);
if (loginResult.Succeeded)
{
var symmetricKey = Convert.FromBase64String(Secret);
var tokenDescriptor = new SecurityTokenDescriptor
{
Subject = new ClaimsIdentity(loginResult.Claims),
Expires = DateTime.UtcNow.AddDays(1),
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(symmetricKey), SecurityAlgorithms.HmacSha256Signature)
};
var stoken = _tokenHandler.CreateToken(tokenDescriptor);
var token = _tokenHandler.WriteToken(stoken);
// Check token here to see if it works
var jwtToken = _tokenHandler.ReadToken(token) as JwtSecurityToken;
loginResult.JwtToken = token;
}
return loginResult;
}
public ClaimsPrincipal ValidateJwtToken(string tokenString)
{
ClaimsPrincipal principal;
try
{
var jwtToken = _tokenHandler.ReadToken(tokenString) as JwtSecurityToken;
if (jwtToken == null)
{
principal = null;
}
else
{
var symmetricKey = Convert.FromBase64String(Secret);
var validationParameters = new TokenValidationParameters()
{
RequireExpirationTime = true,
ValidateIssuer = false,
ValidateAudience = false,
IssuerSigningKey = new SymmetricSecurityKey(symmetricKey)
};
SecurityToken securityToken;
principal = _tokenHandler.ValidateToken(tokenString, validationParameters, out securityToken);
}
}
catch (Exception ex)
{
principal = null;
}
return principal;
}
下面的行完美地读取了令牌,但是当我在第二种方法中实际读取它时,却出现了异常.
The line below reads the token perfectly, however when I actually read it in the 2nd method I get an exception.
// Check token here to see if it works
var jwtToken = _tokenHandler.ReadToken(token) as JwtSecurityToken
我已经验证了这两个字符串是相同的,当我实际上想在我的一生中验证令牌时,我看不到自己在做什么,对此我感到非常困惑.有什么想法吗?
I have verified the two string are identical, I am extremely confused as to why this stops working when I actually want to validate the token for the life of me I can't see what I am doing wrong. Any ideas please?
例外
"IDX10729: Unable to decode the header 'header' as Base64Url encoded string. jwtEncodedString: 'Token here'."
堆栈跟踪:
at System.IdentityModel.Tokens.Jwt.JwtSecurityToken.Decode(String[] tokenParts, String rawData)
at System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler.ReadJwtToken(String token)
at AuthService.ValidateJwtToken(String tokenString) in AuthService.cs:line 57
我遇到此错误,并通过观察错误详细信息发现,原因是未加载Newtonsoft.Json
dll.
I was having this error and found, by observing the error detail, that the cause was that Newtonsoft.Json
dll was not loaded.
System.IdentityModel.Tokens.Jwt.JsonExtensions
试图加载9.0.0.0版的dll,但项目使用的是10.0.0.0版.错误详细信息如下所示:
The System.IdentityModel.Tokens.Jwt.JsonExtensions
was trying to load version 9.0.0.0 of the dll but the project was using version 10.0.0.0. The error detail have something like this:
System.ArgumentException:IDX10729:...无法加载文件或 程序集'Newtonsoft.Json,Version = 9.0.0.0,Culture = neutral, PublicKeyToken = 30ad4fe6b2a6aeed"或其依赖项之一
System.ArgumentException: IDX10729: ... Could not load file or assembly 'Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies
我通过将此绑定添加到配置中来解决:
I resolved by adding this binding to the config:
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-10.0.0.0" newVersion="10.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>