【问题标题】:IdentityServer3 - rejected because invalid CORS pathIdentityServer3 - 由于 CORS 路径无效而被拒绝
【发布时间】:2015-11-01 22:15:43
【问题描述】:

我们有一个 ASP.NET MVC 应用程序正在对 IdentityServer3 进行身份验证而没有问题,但是如果用户在大约 3 分钟后继续使用 AJAX 功能(一切都在 3 分钟前)之前等待,则使用 ApiController 的应用程序的 Web API 部分开始失败看起来不错)。

在 Chrome 中看到的错误是:

XMLHttpRequest 无法加载 https://test-auth.myauthapp.com/auth/connect/authorize?client_id=ecan-farmda…gwLTk5ZjMtN2QxZjUyMjgxNGE4MDg2NjFhZTAtOTEzNi00MDE3LTkzNGQtNTc5ODAzZTE1Mzgw。 请求中不存在“Access-Control-Allow-Origin”标头 资源。因此不允许使用原点“http://test.myapp.com” 访问。

在 IE 上出现以下错误:

SCRIPT7002:XMLHttpRequest:网络错误 0x4c7,操作是 被用户取消。

查看 IdentityServer3 的日志,我看到如下条目:

2015-08-10 16:42 [警告] (Thinktecture.IdentityServer.Core.Configuration.Hosting.CorsPolicyProvider) 对路径发出的 CORS 请求:/connect/authorize from origin: http://test.myapp.com 但因 CORS 路径无效而被拒绝

在 IdentityServer3 Web 应用程序中,我给客户端 AllowedCorsOrigins:

Thinktecture.IdentityServer.Core.Models.Client client = new Thinktecture.IdentityServer.Core.Models.Client()
{
    Enabled = configClient.Enabled,
    ClientId = configClient.Id,
    ClientName = configClient.Name,
    RedirectUris = new List<string>(),
    PostLogoutRedirectUris = new List<string>(),
    AllowedCorsOrigins = new List<string>(),
    RequireConsent = false, // Don't show consents screen to user
    RefreshTokenExpiration = Thinktecture.IdentityServer.Core.Models.TokenExpiration.Sliding
};

foreach (Configuration.RegisteredUri uri in configClient.RedirectUris)
{
    client.RedirectUris.Add(uri.Uri);
}

foreach (Configuration.RegisteredUri uri in configClient.PostLogoutRedirectUris)
{
    client.PostLogoutRedirectUris.Add(uri.Uri);
}

// Quick hack to try and get CORS working
client.AllowedCorsOrigins.Add("http://test.myapp.com");
client.AllowedCorsOrigins.Add("http://test.myapp.com/"); // Don't think trailing / needed, but added just in case

clients.Add(client);

在注册服务时,我添加了一个 InMemoryCorsPolicyService:

app.Map("/auth", idsrvApp =>
{
    var factory = new IdentityServerServiceFactory();

    factory.Register(new Registration<AuthContext>(resolver => AuthObjects.AuthContext));
    factory.Register(new Registration<AuthUserStore>());
    factory.Register(new Registration<AuthRoleStore>());
    factory.Register(new Registration<AuthUserManager>());
    factory.Register(new Registration<AuthRoleManager>());

    // Custom user service used to inject custom registration workflow
    factory.UserService = new Registration<IUserService>(resolver => AuthObjects.AuthUserService);

    var scopeStore = new InMemoryScopeStore(Scopes.Get());
    factory.ScopeStore = new Registration<IScopeStore>(scopeStore);
    var clientStore = new InMemoryClientStore(Clients.Get());
    factory.ClientStore = new Registration<IClientStore>(clientStore);

    var cors = new InMemoryCorsPolicyService(Clients.Get());
    factory.CorsPolicyService = new Registration<ICorsPolicyService>(cors);

    ...

    var options = new IdentityServerOptions
    {
        SiteName = "Authentication",
        SigningCertificate = LoadCertificate(),
        Factory = factory,
        AuthenticationOptions = authOptions
    };

    ...
});

我确实注意到 IdentityServer3 日志条目显示“对路径进行的 CORS 请求:/connect/authorize”而不是“对路径进行的 CORS 请求:/auth/connect/authorize”。但是查看 IdentityServer3 源代码表明这可能不是问题。

也许 InMemoryCorsPolicyService 没有被使用?

关于为什么称为 ApiController 的 AJAX 无法正常工作的任何想法?

Thinktecture.IdevtityServer3 v1.6.2 已使用 NuGet 安装。

更新

我正在与 IdentityServer3 开发人员进行对话,但仍无法解决问题。如果有帮助:

https://github.com/IdentityServer/IdentityServer3/issues/1697

【问题讨论】:

    标签: c# ajax cors asp.net-apicontroller identityserver3


    【解决方案1】:

    您是否也尝试添加 https url?- client.AllowedCorsOrigins.Add("https://test.myapp.com");

    【讨论】:

    • 早在 2015 年,https 并不是那么强制性。
    猜你喜欢
    • 2021-05-07
    • 1970-01-01
    • 2012-10-28
    • 1970-01-01
    • 2016-11-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多