c# - 承载错误 - invalid_token - 找不到签名 key

标签 c# angular asp.net-core jwt azure-active-directory

我有一个与 .Net Core 2.2 API 后端连接的 Angular 7 应用程序。这是与 Azure Active Directory 的接口(interface)。

在 Angular 7 方面,它正在使用 AAD 进行正确身份验证,并且我得到了一个有效的 JWT,已在 jwt.io 上进行了验证。 .

在 .Net Core API 端,我创建了一个简单的测试 API,其中包含 [Authorize]

当我从 Angular 调用此方法时,添加承载 token 后,我得到(如 Chrome 调试工具、网络选项卡、“ header ”中所示):

WWW-Authenticate: Bearer error="invalid_token", error_description="The signature key was not found"

HTTP/1.1 401 未经授权

简单的测试 API 是:

    [Route("Secure")]
    [Authorize]
    public IActionResult Secure() => Ok("Secure works");

Angular 调用代码也非常简单:

    let params : any = {
        responseType: 'text',
        headers: new HttpHeaders({
            "Authorization": "Bearer " + token,
            "Content-Type": "application/json"
        })
    }

    this.http
        .get("https://localhost:5001/api/azureauth/secure", params)
        .subscribe(
            data => { },
            error => { console.error(error); }
        );

如果我删除 [Authorize] 属性,并将其作为来自 Angular 的标准 GET 请求,它可以正常工作

我的 Startup.cs 包含:

        services
            .AddAuthentication(AzureADDefaults.AuthenticationScheme)
            .AddAzureADBearer(options => this.Configuration.Bind("AzureAd", options));

appsettings.json 中的选项均已正确设置(例如 ClientId、TenantId 等),并且此处的 options 按预期填充。

最佳答案

我也遇到了同样的问题。我缺少权限..确保权限和 api 名称现在正确,启动文件中配置服务中的这段代码对我有用:

services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme)
                .AddIdentityServerAuthentication( x =>
                {
                    x.Authority = "http://localhost:5000"; //idp address
                    x.RequireHttpsMetadata = false;
                    x.ApiName = "api2"; //api name
                });

关于c# - 承载错误 - invalid_token - 找不到签名 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58563661/

相关文章:

c# - C# 中的 OFX​​ 文件解析器

c# - 声明并初始化变量以分配匿名类型列表

angular - 混洗从 Observable 返回的值

node.js - 我需要在生产服务器上安装 Node.js 来托管 Angular 2 吗?

visual-studio-2015 - 将 Angular Material 添加到 Visual Studio 2015 中的 ASP.NET CORE 项目

asp.net - Cloud Optimized ASP.Net vNext 和普通版有什么区别?

c# - PropertyChangedEventHandler 是如何工作的?

c# - 从 Windows 10 1709 开始优雅地重新启动 explorer.exe,Fall Creators Update aka Redstone 3

javascript - 使用 Angular FormControlls 时将 JavaScript 对象从平面对象标准化为嵌套对象

asp.net-core - Gitlab CI : Skipping cache archiving due to empty cache key