我们有一个 ASP.NET MVC 应用程序正在对 IdentityServer3 进行身份验证而没有问题,但是如果用户在大约 3 分钟后继续使用 AJAX 功能之前等待,则使用 ApiController 的应用程序的 Web API 部分开始失败(在 3 分钟之前一切似乎都很好) .
在 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