asp.net - 如何在 .NET 3.5 中使用 System.IdentityModel.Tokens.Jwt 5.2.1?

标签 asp.net .net-3.5 jwt .net-4.5

我有一个项目,我想从请求中验证 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/

相关文章:

c# - .NET Framework 4 与 .NET Framework 3.5 中的 WCF 有哪些新功能?

c# - yield return with try catch,我该如何解决

c# - C# 中的电子邮件调度 (ASP .NET)

vb.net - 在 VB.NET 中连接数组

asp.net - 安装 VS Express 时出现 Web 平台安装程序错误

Angular 2 无法在 XMLHttpRequest : Invalid URL 上执行打开

authentication - 如何使用 JWT token 管理多设备同时登录?

asp.net-web-api - 每个资源服务器的 JWT 多个受众

javascript - 如何在asp.net中获取数组中的值(或者asp-classic会更好)

c# - ASP.NET DateTime 无法正确转换