.Net Core Web API Bearer 颁发者无效

DJI*_*ave 5 jwt identityserver4 blazor webapi

我已经根据最新的 Microsoft 模板编写了 Blazor WASM 应用程序。在开发模式下,一切都很好,但将其发布到 Azure 应用服务后,在调用 API 时,我随机收到 401 未经授权,查看返回的标头

WWW-Authenticate: Bearer error="invalid_token", error_description="The issuer 'https://*domain*.azurewebsites.net' is invalid"
Run Code Online (Sandbox Code Playgroud)

这是当客户端使用 https://.azurewebsites.net 客户端时。所以它与Web API相匹配。

我还有一个附加到应用程序服务的自定义域,这意味着还有 https://www. 域名.co.uk 和 https://域名.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");
        });
    }
Run Code Online (Sandbox Code Playgroud)

任何正确方向的帮助或提示都值得赞赏

干杯

戴夫

Ant*_*ony 0

就我而言,这是由App Service的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
  });
Run Code Online (Sandbox Code Playgroud)