gpt4 book ai didi

c# - JWT 无法将 header 解码为 Base64Url 编码字符串

转载 作者:太空狗 更新时间:2023-10-29 18:26:31 24 4
gpt4 key购买 nike

我有以下代码:

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;
}

下面的行完美地读取了 token ,但是当我在第二种方法中实际读取它时,我得到了一个异常。

// Check token here to see if it works
var jwtToken = _tokenHandler.ReadToken(token) as JwtSecurityToken

我已经验证了这两个字符串是相同的,我非常困惑为什么当我真的想为我的生命验证 token 时它停止工作我看不出我做错了什么。有什么想法吗?

编辑:

异常

   "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 没有加载。

System.IdentityModel.Tokens.Jwt.JsonExtensions 试图加载 9.0.0.0 版的 dll,但项目使用的是 10.0.0.0 版。错误详细信息如下所示:

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

我通过将此绑定(bind)添加到配置来解决:

 <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>

关于c# - JWT 无法将 header 解码为 Base64Url 编码字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43003502/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com