【问题标题】:ASP Net Core IdentityServer, "The issuer is invalid" on production environmentASP Net Core IdentityServer,生产环境中的“发行者无效”
【发布时间】:2020-03-16 21:30:56
【问题描述】:

我正在尝试在生产环境(AWS Elasticbeanstalk 服务器)上部署一个使用 IdentityServer 的简单 asp 网络核心项目;我的测试项目基本上是启用了身份验证的 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,所以我认为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");
            }
        });
    }

appsetting.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 吗?

标签: amazon-elastic-beanstalk identityserver4 bearer-token jwt-auth asp.net-core-3.1


【解决方案1】:

在您的启动中设置您的域地址

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

【讨论】:

  • 另一种方法是正确设置应用程序以坐在负载均衡器后面。您需要使用 X-Forwarded-For 和 X-Forwarded-Proto 标头并更新当前请求上下文(假设 AWS 以正常方式进行)
  • 这似乎是正确的解决方案,但我不明白为什么我需要设置它。 Here 他们建议不要设置此属性,因为该值是由客户端请求推断的。
  • 要调查原因,并寻找另一种方法,解码您的 access_token 的有效负载并查看它的发行者。这将有助于找到此错误的原因。
猜你喜欢
  • 2022-08-17
  • 1970-01-01
  • 2021-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-13
  • 1970-01-01
  • 2022-01-17
相关资源
最近更新 更多