amazon-elastic-beanstalk - 生产环境中的 ASP Net Core IdentityServer, "The issuer is invalid"

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

我正在尝试在生产(AWS Elasticbeanstalk 服务器)上部署一个使用 IdentityServer 的简单 asp 网络核心项目;我的测试项目基本上是启用了身份验证的 Visual Studio 2019 的 React.js 模板。

在开发中一切正常,但在生产中,当尝试使用 jwt token 对我的 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": "*"
    }

最佳答案

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

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

关于amazon-elastic-beanstalk - 生产环境中的 ASP Net Core IdentityServer, "The issuer is invalid",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60713473/

相关文章:

java - ElasticBeanstalk - 更改 java.security?

amazon-elastic-beanstalk - Elastic Beanstalk 找不到 server.js 文件

asp.net - DelegatingHandler 将授权 token 添加到请求

kubernetes - kubectl --token=$TOKEN 没有使用 token 的权限运行

azure - C# - 获取图形访问 token - 使用客户端 ID、客户端 key 、范围和客户端委托(delegate)权限

amazon-web-services - 如何为 Amazon AWS 部署设置 Composer 标志 --no-dev

amazon-web-services - aws_beanstalk/ubuntu 的拉取访问被拒绝

c# - 使用客户端凭证流持久化 token 的最佳实践

c# - 拦截 ASP.net Core 2.0 管道中的 401 错误

scope - ApiResource 中使用的 Claims 和 IdentityServer4 中的 Scope 有什么不同