ASP Net Core IdentityServer,生产环境中出现“发行者无效”的问题

9
我正在尝试在生产环境(AWS Elasticbeanstalk服务器)部署一个使用IdentityServer的简单ASP.NET Core项目;我的测试项目基本上是启用了身份验证的Visual Studio 2019的React.js模板。
在开发中一切正常,但在生产中,当我尝试使用jwt令牌对我的API进行身份验证时出现错误。
WWW-Authenticate: Bearer error="invalid_token", error_description="The issuer 'http://***.elasticbeanstalk.com' is invalid"

使用的access_token是从调用返回的。
POST http://***.elasticbeanstalk.com/connect/token

奇怪的行为是以下请求到
GET http://***.elasticbeanstalk.com/connect/userinfo

它正确返回用户数据,这里使用access_token,所以我认为令牌是正确的。

不幸的是,对我的api的请求失败了,出现了上面的错误。

我的Startup.cs代码如下:

   public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(
                Configuration.GetConnectionString("DefaultConnection")));

        services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)
            .AddEntityFrameworkStores<ApplicationDbContext>();

        services.AddIdentityServer()
            .AddApiAuthorization<ApplicationUser, ApplicationDbContext>();

        services.AddAuthentication()
            .AddIdentityServerJwt();

        services.AddControllersWithViews();
        services.AddRazorPages();

        // In production, the React files will be served from this directory
        services.AddSpaStaticFiles(configuration =>
        {
            configuration.RootPath = "ClientApp/build";
        });
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseDatabaseErrorPage();
        }
        else
        {
            app.UseExceptionHandler("/Error");
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseSpaStaticFiles();

        app.UseRouting();

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

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller}/{action=Index}/{id?}");
            endpoints.MapRazorPages();
        });

        app.UseSpa(spa =>
        {
            spa.Options.SourcePath = "ClientApp";

            if (env.IsDevelopment())
            {
                spa.UseReactDevelopmentServer(npmScript: "start");
            }
        });
    }

appsettings.json 文件包含如下内容:

    {
      "ConnectionStrings": {
        "DefaultConnection": "***"
      },
      "Logging": {
          "LogLevel": {
          "Default": "Information",
          "Microsoft": "Warning",
          "Microsoft.Hosting.Lifetime": "Information"
          }
        },
      "IdentityServer": {
        "Clients": {
          "myapp": {
            "Profile": "IdentityServerSPA",
            "RedirectUris": [ "/signin-oidc" ]
          }
        },
        "Key": {
          "Type": "Store",
          "StoreName": "My",
          "StoreLocation": "LocalMachine",
          "Name": "CN=http://***.elasticbeanstalk.com"
        }
      },
    "AllowedHosts": "*"
    }


可能与此无关,但不应该是HTTPS吗? - mackie
1个回答

14

在你的创业公司中设置你的域名地址。

        services.AddIdentityServer(options =>
        {
            options.IssuerUri = "http://***.elasticbeanstalk.com";
        })

一种替代方案是为应用程序正确设置坐在负载均衡器后面。您需要使用 X-Forwarded-For 和 X-Forwarded-Proto 标头并更新当前请求上下文(假设AWS以正常方式执行)。 - mackie
3
这似乎是正确的解决方案,但我不明白为什么需要进行设置。这里建议不要设置该属性,因为该值可以通过客户端请求推断出来。 - blow
1
为了调查原因并可能找到另一种方法,请解码您的access_token的有效负载并查看其发行者。这将有助于找到此错误的原因。 - Mehrdad
1
@blow,你最终弄清楚为什么这是必要的了吗?尽管文档说不需要。 - Aviad P.
@mackie 如果客户端通过HTTPS连接获取到的令牌(发行者为“https://example.com/identity”)被内部服务使用,并与`http://internal.host.name.local`进行通信,会有什么情况? - Simone

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接