微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

IdentityServer4 CORS 设置与 EntityFramework

如何解决IdentityServer4 CORS 设置与 EntityFramework

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

enter image description here

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

enter image description here

进一步研究(我手动定义了内容策略),这是我遇到的错误

enter image description here

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

我使用 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,因为我看到的所有示例都在内存示例中。我认为这个属性应该在客户端表的数据库中......

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

Database Table

最后这是控制台中的错误

Console Error

解决方法

问题在于 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;
    }
}
,

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

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。