ASP.NET拒绝使用jsonwebtoken生成的JWT。
创始人
2024-09-19 00:31:47
0

要解决ASP.NET拒绝使用jsonwebtoken生成的JWT的问题,您可以使用System.IdentityModel.Tokens.Jwt库来生成JWT。下面是一个代码示例来生成JWT:

using System;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
using Microsoft.IdentityModel.Tokens;

public class JwtTokenGenerator
{
    public string GenerateJwtToken(string secretKey, string issuer, string audience, string username, int expireMinutes = 60)
    {
        var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secretKey));
        var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);

        var claims = new[]
        {
            new Claim(JwtRegisteredClaimNames.Sub, username),
            new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString())
        };

        var token = new JwtSecurityToken(
            issuer: issuer,
            audience: audience,
            claims: claims,
            expires: DateTime.UtcNow.AddMinutes(expireMinutes),
            signingCredentials: credentials
        );

        return new JwtSecurityTokenHandler().WriteToken(token);
    }
}

使用示例:

string secretKey = "YourSecretKey";
string issuer = "YourIssuer";
string audience = "YourAudience";
string username = "JohnDoe";

JwtTokenGenerator tokenGenerator = new JwtTokenGenerator();
string jwtToken = tokenGenerator.GenerateJwtToken(secretKey, issuer, audience, username);

Console.WriteLine(jwtToken);

请替换"YourSecretKey"、"YourIssuer"、"YourAudience"为您自己的值,并根据您的需要调整expireMinutes的值。

相关内容

热门资讯

5分钟了解!吉祥麻将软件黑科技... 5分钟了解!吉祥麻将软件黑科技,中至赣牌圈棋牌有挂吗,AA德州教程(有挂技巧);1、超多福利:超高返...
八分钟了解!荆门麻将app脚本... 八分钟了解!荆门麻将app脚本,闽游十三水好牌率,细节揭秘(有挂规律)在进入闽游十三水好牌率辅助挂后...
一分钟了解!易棋游戏外 挂,欢... 一分钟了解!易棋游戏外 挂,欢乐达人有没有外挂,可靠教程(有挂细节)1、全新机制【欢乐达人有没有外挂...
二分钟了解!陕西欢喜麻将其实是... 二分钟了解!陕西欢喜麻将其实是有挂的,决战手机版外挂,扑克教程(有挂规律)陕西欢喜麻将其实是有挂的辅...
五分钟了解!28圈辅助工具,皮... 五分钟了解!28圈辅助工具,皮皮跑得快辅助器免费版,德州论坛(有挂神器)1、金币登录送、破产送、升级...
九分钟了解!全民比鸡怎么能赢,... 九分钟了解!全民比鸡怎么能赢,钱塘十三水辅助软件出售,存在挂教程(有挂软件);1、钱塘十三水辅助软件...
9分钟了解!旺旺江苏手机麻将有... 9分钟了解!旺旺江苏手机麻将有挂吗,新蜜瓜牛牛辅助器,详细教程(有挂揭秘)进入游戏-大厅左侧-新手福...
九分钟了解!网易棋牌输赢有规律... 九分钟了解!网易棋牌输赢有规律吗,福麻圈有挂吗,技巧教程(有挂攻略)1、完成网易棋牌输赢有规律吗透视...
四分钟了解!哈局十三张,大赢家... 四分钟了解!哈局十三张,大赢家跑得快辅助,分享教程(有挂脚本)1、大赢家跑得快辅助系统规律教程、大赢...
8分钟了解!星星武汉麻将胡牌技... 8分钟了解!星星武汉麻将胡牌技巧,赣牌圈开挂是真的吗,科技教程(有挂揭秘)1、进入游戏-大厅左侧-新...