【问题标题】:.Net Core Web API Bearer The issuer is invalid.Net Core Web API Bearer 发行者无效
【发布时间】:2022-03-12 23:25:03
【问题描述】:

我已经基于最新的 Microsoft 模板编写了 Blazor WASM 应用程序。在开发模式下一切正常,但在将其发布到 Azure App Service 后,我在调用 API 时随机获得 401 未经授权,查看我得到的返回标头

WWW-Authenticate: Bearer error="invalid_token", error_description="The issuer 'https://*domain*.azurewebsites.net' is invalid"

这是客户端使用 https://domain.azurewebsites.net 客户端的情况。所以它与 Web API 相匹配。

我还有一个附加到应用服务的自定义域,这意味着还有 https://www.domain.co.uk 和 https://domain.co.uk 都是 SSL 的。

我检查了 JWT 令牌,它包含我正在调用的网站版本的正确 URL。

有时一切正常,但在 60% 的情况下,它允许用户登录,然后在 API 调用上失败。我似乎无法将其跟踪到 1 个域名或模式,例如过期登录。如果您注销然后重新登录,则无法解决问题。

配置如下所示

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseDatabaseErrorPage();
            app.UseWebAssemblyDebugging();
        }
        else
        {
            app.UseExceptionHandler("/Error");
        }

        app.UseHttpsRedirection();
        app.UseBlazorFrameworkFiles();
        app.UseStaticFiles();

        app.UseRouting();

        app.UseIdentityServer();
        app.UseAuthentication();
        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapRazorPages();
            endpoints.MapControllers();
            endpoints.MapFallbackToFile("index.html");
        });
    }

对正确方向的任何帮助或提示表示赞赏

干杯

戴夫

【问题讨论】:

  • 你知道了吗,戴夫,我在用于容器 (linux) 的 Azure Web 应用程序中运行 IS4 时遇到了同样的问题
  • 嗨,J King,没有深入了解这一点,我最终为每个 URL 创建了一个 Web 应用程序,并将每个 Web 应用程序最小化为 1 个 URL。这可行,但不是一个很好的解决方案

标签: jwt identityserver4 blazor webapi


【解决方案1】:

在我的情况下,它是由应用服务的 Linux 环境引起的。现在文档对​​此有明确的说明: For Azure App Service deployments on Linux, specify the issuer explicitly in Startup.ConfigureServices.

我是这样设置的:

services.Configure<JwtBearerOptions>(
  IdentityServerJwtConstants.IdentityServerJwtBearerScheme, 
  options =>
  {
    options.Authority = "https://my-site.azurewebsites.net";
#if DEBUG
    options.Authority = "https://localhost:5001";
#endif
  });

【讨论】:

    猜你喜欢
    • 2020-06-03
    • 2017-02-07
    • 2019-12-12
    • 2021-02-06
    • 1970-01-01
    • 1970-01-01
    • 2020-06-27
    • 2020-03-04
    • 2013-11-30
    相关资源
    最近更新 更多