我有一个项目,我想从请求中验证 token 。我的项目的 .NETFramework 版本是 3.5 .我试图在我的项目和这些代码段中使用以下代码,我在另一个项目中使用了验证 token ,其中 .NETFramework 版本为 4.5.2 .
using Microsoft.IdentityModel.Tokens;
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TokenValidator
{
public class TokenValidator
{
public static bool IsTokenValid(string token, string secretKey)
{
string tokenPGID;
JwtSecurityToken validatedToken;
try
{
validatedToken = ValidateToken(token, secretKey) as JwtSecurityToken;
return true;
}
catch (Exception)
{
return false;
}
}
private static SecurityToken ValidateToken(string token, string secretKey)
{
var handler = new JwtSecurityTokenHandler();
var tokenS = handler.ReadToken(token) as JwtSecurityToken;
TokenValidationParameters tokenValidationParameters = new TokenValidationParameters
{
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secretKey)),
RequireExpirationTime = true,
ValidateLifetime = true,
ValidateAudience = false,
ValidateActor = false,
ValidateIssuer = false
};
SecurityToken securityToken = new JwtSecurityToken();
handler.ValidateToken(token, tokenValidationParameters, out securityToken);
return securityToken;
}
}
}
但是,我在我的项目中遇到以下错误:
Error Could not install package 'System.IdentityModel.Tokens.Jwt 5.2.1'. You are trying to install this package into a project that targets '.NETFramework,Version=v3.5', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.
有什么方法可以在 .NETFramework 3.5 的项目中验证 JWT token 意味着我应该使用这个包的哪个版本来与 .NET 3.5 兼容?
有没有其他方法可以在 .NET 3.5 中验证 JWT token ?
最佳答案
我认为没有微软官方支持为 .Net 3.5 生成 JWT,但您可以使用第三方库,如 Auth0 开发的库:https://libraries.io/github/auth0/jwt
关于asp.net - 如何在 .NET 3.5 中使用 System.IdentityModel.Tokens.Jwt 5.2.1?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49976663/