You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为SPA配置IdentityServer4时遇到签名密钥未找到错误

问题根因与修复步骤

你遇到的The signature key was not found错误以及User.Identity为空的问题,是由配置冲突、中间件顺序错误、校验规则不合理三个核心问题导致的,按以下步骤修改即可修复:

1. 移除重复的JWT认证配置

你同时注册了两套JWT认证方案,自定义的Bearer方案和IdentityServerJwt自带的认证方案冲突,导致验证token时无法正常获取IdentityServer的签名公钥。
删除以下冗余配置:

// 删掉这段整个自定义JWT配置
services.AddAuthentication("Bearer")
    .AddJwtBearer("Bearer", options =>
    {
        options.Authority = Configuration.GetValue<string>("Authority");
        options.TokenValidationParameters = new TokenValidationParameters
        {
            ValidateAudience = false
        };
    })

修改为仅保留:

services.AddAuthentication()
    .AddIdentityServerJwt();

AddIdentityServerJwt会自动同步IdentityServer的签名密钥、配置,适配同站点部署的API验证场景,不需要额外自定义JWT配置。

2. 修正中间件注册顺序

UseIdentityServer需要放在UseAuthentication之前执行,才能提前注入认证所需的密钥、配置信息。修改Configure方法的中间件顺序为:

app.UseRouting();

app.UseIdentityServer(); // 移到UseAuthentication前面
app.UseAuthentication();
app.UseAuthorization();

3. 修正授权策略的Scope校验规则

你当前将三个Scope拼接为一个字符串做匹配,和token中多值存储的Scope结构不匹配,会导致授权校验失败。修改授权策略配置:

options.AddPolicy("Accounting.ApplicationAPI", policy =>
{
    policy.RequireAuthenticatedUser();
    // 仅校验API所需的业务Scope,openid、profile是身份认证类Scope,不需要在API层校验
    policy.RequireScope("Accounting.ApplicationAPI");
});

4. 修复配置笔误与生产环境适配

  • 你代码中services.AddControllers();末尾多了一个分号,会导致编译错误,删除该分号,保留后面的.AddMvcOptions链式调用。
  • 生产环境不要使用AddDeveloperSigningCredential(),请生成正式的pfx签名证书,用AddSigningCredential加载证书,避免签名密钥丢失、重启后旧token失效的问题。
  • 确认配置文件中Authority的值和IdentityServer实际访问地址完全一致,协议(http/https)、域名、端口都要匹配,末尾不要加多余的斜杠。

内容的提问来源于stack exchange,提问作者Rusty

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 22:57:03