Angular和.Net Core中的JWT身份验证令牌的受众和发行者无效,并且缺少范围。
创始人
2024-10-26 13:31:35
0

在Angular和.Net Core中,JWT身份验证令牌的受众和发行者无效,并且缺少范围的问题可以通过以下步骤来解决:

  1. 首先,确保你的Angular应用程序和.Net Core应用程序都正确配置了JWT身份验证。

在Angular应用程序的环境配置文件中,确保指定了正确的后端API地址作为受众(audience)。

export const environment = {
  production: false,
  apiUrl: 'https://your-api-url.com' // 替换为你的后端API地址
};

在.Net Core应用程序的Startup.cs文件中,确保正确配置了JWT身份验证的发行者(issuer)和密钥。

services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
    .AddJwtBearer(options =>
    {
        options.TokenValidationParameters = new TokenValidationParameters
        {
            ValidateIssuer = true,
            ValidIssuer = "https://your-issuer-url.com", // 替换为你的发行者URL
            ValidateAudience = true,
            ValidAudience = "https://your-audience-url.com", // 替换为你的受众URL
            ValidateIssuerSigningKey = true,
            IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("your-secret-key")) // 替换为你的密钥
        };
    });
  1. 确保JWT令牌中包含了正确的受众和发行者。

在Angular应用程序中,确保使用正确的受众和发行者创建JWT令牌。

import { JwtHelperService } from '@auth0/angular-jwt';

const helper = new JwtHelperService();
const token = 'your-jwt-token'; // 替换为你的JWT令牌
const decodedToken = helper.decodeToken(token);
const audience = 'https://your-audience-url.com'; // 替换为你的受众URL
const issuer = 'https://your-issuer-url.com'; // 替换为你的发行者URL

if (decodedToken.aud !== audience || decodedToken.iss !== issuer) {
  // 令牌的受众或发行者无效
  // 处理错误逻辑
} else {
  // 令牌有效
  // 执行其他操作
}

在.Net Core应用程序中,确保在生成JWT令牌时包含了正确的受众和发行者。

var claims = new[]
{
    new Claim(JwtRegisteredClaimNames.Sub, "your-subject"), // 替换为你的主题
    new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()), // 替换为你的唯一标识符
    new Claim(JwtRegisteredClaimNames.Iat, DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString(), ClaimValueTypes.Integer64), // 替换为当前时间的Unix时间戳
    new Claim(JwtRegisteredClaimNames.Aud, "https://your-audience-url.com"), // 替换为你的受众URL
    new Claim(JwtRegisteredClaimNames.Iss, "https://your-issuer-url.com") // 替换为你的发行者URL
};

var token = new JwtSecurityToken(
    claims: claims,
    expires: DateTime.UtcNow.AddDays(1), // 设置过期时间
    signingCredentials: new SigningCredentials(new SymmetricSecurityKey(Encoding.UTF8.GetBytes("your-secret-key")) , SecurityAlgorithms.HmacSha256) // 替换为你的密钥
);

var jwtToken = new JwtSecurityTokenHandler().WriteToken(token);
  1. 如果仍然出现范围(scope)缺失的问题,你需要确保在生成JWT令牌时包含了正确的范围。

在.Net Core应用程序中,可以将范围添加为令牌的自定义声明(Claim)。

var claims = new[]
{
    // ...
    new Claim("scope", "your-scope") // 替换为你的范围
    // ...
};

然后,在.Net Core应用程序的Startup.cs文件中,使用AddJwtBearer方法的ScopeClaim属性来配置正确的范围。

相关内容

热门资讯

透视烘培!aa poker辅助... 透视烘培!aa poker辅助,AApoker透视软件总是有工具,八分钟教程(有挂存在)aa pok...
透视总结!wpk辅助哪里买,w... 透视总结!wpk辅助哪里买,wpk控制牌是真的吗(透视)果然有技巧(哔哩哔哩)1、下载好透视辅助下载...
透视策略!德普之星辅助软件,德... 透视策略!德普之星辅助软件,德普之星工具切实存在有插件,第2分钟教程(有挂教学)德普之星辅助软件能透...
一直以来!aapoker透视插... 一直以来!aapoker透视插件,微信小程序微乐破解器2024,揭幕透视(新版有挂)-哔哩哔哩1、首...
透视好友!pokemmo手机脚... 透视好友!pokemmo手机脚本辅助器,微乐贵阳足鸡麻将开挂,科普透视(今日头条)-哔哩哔哩poke...
透视积累!德普之星透视辅助软件... 透视积累!德普之星透视辅助软件下载,德普之星私人局总是是有透视,第3分钟教程(确实有挂)1、玩家可以...
透视手段!wpk辅助哪里买,W... 透视手段!wpk辅助哪里买,WPK有透视一直真的是有工具,7分钟教程(有挂透视)1、玩家可以在wpk...
这一现象值得深思!xpoker... 这一现象值得深思!xpoker辅助器,微乐自建房辅助软件功能,总结脚本(有挂解惑)-哔哩哔哩xpok...
透视绝活!wepoker软件辅... 透视绝活!wepoker软件辅助程序,WePoKer透视果然真的是有工具,8分钟教程(有挂秘诀)1、...
今日!wepoker好友房开挂... 今日!wepoker好友房开挂,微乐春天扑克开挂辅助插件,开挂脚本(有挂存在)-哔哩哔哩1、上手简单...