Angular和.Net Core中的JWT身份验证令牌的受众和发行者无效,并且缺少范围。
创始人
2024-10-26 13:33:13
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属性来配置正确的范围。

相关内容

热门资讯

安装apache-beam==... 出现此错误可能是因为用户的Python版本太低,而apache-beam==2.34.0需要更高的P...
避免在粘贴双引号时向VS 20... 在粘贴双引号时向VS 2022添加反斜杠的问题通常是由于编辑器的自动转义功能引起的。为了避免这个问题...
Android Recycle... 要在Android RecyclerView中实现滑动卡片效果,可以按照以下步骤进行操作:首先,在项...
omi系统和安卓系统哪个好,揭... OMI系统和安卓系统哪个好?这个问题就像是在问“苹果和橘子哪个更甜”,每个人都有自己的答案。今天,我...
原生ios和安卓系统,原生对比... 亲爱的读者们,你是否曾好奇过,为什么你的iPhone和安卓手机在操作体验上有着天壤之别?今天,就让我...
Android - 无法确定任... 这个错误通常发生在Android项目中,表示编译Debug版本的Java代码时出现了依赖关系问题。下...
Android - NDK 预... 在Android NDK的构建过程中,LOCAL_SRC_FILES只能包含一个项目。如果需要在ND...
Akka生成Actor问题 在Akka框架中,可以使用ActorSystem对象生成Actor。但是,当我们在Actor类中尝试...
Agora-RTC-React... 出现这个错误原因是因为在 React 组件中使用,import AgoraRTC from “ago...
Alertmanager在pr... 首先,在Prometheus配置文件中,确保Alertmanager URL已正确配置。例如:ale...