【问题标题】:IdentityServer4 CORS Settings with EntityFrameworkIdentityServer4 CORS 设置与 EntityFramework
【发布时间】:2021-06-22 10:23:12
【问题描述】:

编辑: 我部分回答了我的问题。对于那些好奇的人,数据库中有另一个名为ClientProperties 的表,您可以使用它来设置不在主客户端表上的其他属性。我这样做了,但我仍然有一个错误:

我的客户端配置条目如下所示:

进一步处理它(我手动定义了内容策略),这是我遇到的错误:

--------- 原始问题---------

我使用 EntityFramework 为我的配置和操作数据设置了 IdentityServer4。

我在登录应用程序时尝试允许 ajax 处理重定向时遇到了问题。我没有尝试对所有内容进行硬编码,而是尝试在 IDS4 中设置 CORS。

我正在阅读文档HERE。它谈到在客户端上设置AllowedCorsOrigins,以便身份服务器正确设置CORS。我遇到的问题是,在数据库中,客户端没有定义名为AllowedCorsOrigins 的列。事实上,openId model 上定义了很多缺失的列。

在我的身份服务器上,我的 DI 配置如下:

services.AddIdentity < IdentityUser, IdentityRole > ()
  .AddUserManager < ApplicationUserManager < IdentityUser >> ()
  .AddSignInManager < ApplicationSignInManager < IdentityUser >> ()
  .AddEntityFrameworkStores < ApplicationDbContext > ();

services.AddIdentityServer()
  .AddAspNetIdentity < IdentityUser > ()
  .AddConfigurationStore(options => {
    options.ConfigureDbContext = builder => builder.UseSqlServer(
      configuration.GetConnectionString("IdentityConnection"),
      opt => opt.MigrationsAssembly(startupName));
  })
  .AddOperationalStore(options => {
    options.ConfigureDbContext = builder => builder.UseSqlServer(
      configuration.GetConnectionString("IdentityConnection"),
      opt => opt.MigrationsAssembly(startupName));
  })
  .AddProfileService < ApplicationProfileService < IdentityUser >> ()
  .AddDeveloperSigningCredential();

在我的客户端上,DI 设置如下:

services.AddAuthentication(options => {
    options.DefaultScheme = "cookie";
    options.DefaultChallengeScheme = "oidc";
  }).AddCookie("cookie")
  .AddOpenIdConnect("oidc", options => {
    options.Authority = appSettings.App.AuthorityUrl;
    options.ClientId = appSettings.App.ClientId;
    options.ClientSecret = appSettings.App.ClientSecret;

    options.ResponseType = ResponseTypes.Code;
    options.UsePkce = true;
    options.ResponseMode = "query";

    options.GetClaimsFromUserInfoEndpoint = true;
    options.ClaimActions.MapJsonKey("role", "role", "role");
    options.ClaimActions.MapJsonKey("nickname", "nickname", "nickname");
    options.ClaimActions.MapJsonKey(JwtClaimTypes.Picture, JwtClaimTypes.Picture, JwtClaimTypes.Picture);
    options.TokenValidationParameters.RoleClaimType = "role";

    var test = options.ClaimActions;

    options.Scope.Add(appSettings.App.Scopes[0]);
    options.SaveTokens = true;
  });

我无法确定应该在哪里定义AllowedCorsOrigins,因为我看到的所有示例都在内存示例中。我认为这个属性应该在客户端表的数据库中......

以下是我可以访问数据库中的哪些值:

最后这是控制台中的错误:

【问题讨论】:

  • 你用什么浏览器?您是否在 Chrome 上收到此错误?
  • @nahidf 我刚刚发现了这个问题,ajax 帖子不会让你做 302 重定向。我必须以不同的方式处理这个问题。

标签: c# entity-framework identityserver4 openid


【解决方案1】:

如果您尝试定义客户可能请求登录的域,您需要在ClientCorsOrigins 表中进行设置。在那里,您将定义一个网址,例如 https://www.example.com 和来自 Clients 表的客户端 ID。

但是,如果您尝试为针对 Identity Server 的其他 API 调用定义 CORS 配置,则需要在您的 Startup.cs 中定义它

services.AddCors(options =>
{
    options.AddPolicy(name: MyAllowSpecificOrigins,
        builder =>
        {
            builder.WithOrigins("http://example.com",
                "http://www.contoso.com");
        });
});

【讨论】:

  • 已经尝试过所有这些。看我之前的回答。无法在 AJAX 发布响应中执行 302 重定向。不过谢谢!对其他人来说仍然是很好的信息。
【解决方案2】:

问题在于 AJAX。

因为我在 MVC 中向登录端点发布了一个表单,所以响应不能是 302 重定向。这是因为 AJAX 不允许对帖子进行重定向。

为了解决这个问题,我现在返回一个带有返回 URL 的 JSON 对象,并让 javascript 设置 window.location 值。下面是一个例子(注意我首先检查它是否是一个 JSON 对象,因为这个处理程序处理多个 ajax 调用)。

if (xhr.responseText.startsWith('{')) {
    let json = JSON.parse(xhr.responseText);
    if (json.ok) {
        window.location = json.redirect;
        return;
    }
}

【讨论】:

    猜你喜欢
    • 2021-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-07
    • 1970-01-01
    • 2020-02-21
    • 2017-12-17
    • 1970-01-01
    相关资源
    最近更新 更多