gpt4 book ai didi

c# - 身份服务器 4 - 配置了允许的来源但不允许来源

转载 作者:行者123 更新时间:2023-12-03 16:24:37 30 4
gpt4 key购买 nike

我在同一个项目中有一个 SPA 和 API,当我向 API 发出请求时,我不断收到以下错误。

AllowedOrigins configured and origin http://localhost:5000 is not allowed
CorsPolicyService did not allow origin: http://localhost:5000
api的路径是: http://localhost:5000 .我确保我有 ClientCorsOrigins 中指定的原点客户端的表,我还在 Startup.cs 中添加了一个策略:
services.AddCors(options =>
{
options.AddPolicy("default", policy =>
{
policy.WithOrigins("http://localhost:5000")
.AllowAnyHeader()
.AllowAnyMethod();
});
});
我已经多次检查文档和配置,但当我在 ClientCorsOrigins 表中指定了原点时,我无法弄清楚为什么我会遇到这个问题。我正在使用谷歌浏览器。

最佳答案

您需要使用您的 ClientId 和 Origin 在 [dbo].[ClientCorsOrigin] 表中添加一行/记录。

CorsPolicyProvider.cs 有一个检查线 #62 :

if (await corsPolicyService.IsOriginAllowedAsync(origin))

当它返回 false 时,您的“CorsPolicyService 不允许来源: http://localhost:5000”消息在 #69 行中.

我假设您正在使用 IdentityServer4.EntityFramework。这是来自位于 IdentityServer4.EntityFramework.Services 命名空间中的 CorsPolicyService.cs 的 IsOriginAllowedAsync 方法:
    /// <summary>
/// Determines whether origin is allowed.
/// </summary>
/// <param name="origin">The origin.</param>
/// <returns></returns>
public Task<bool> IsOriginAllowedAsync(string origin)
{
// doing this here and not in the ctor because: https://github.com/aspnet/CORS/issues/105
var dbContext = _context.HttpContext.RequestServices.GetRequiredService<IConfigurationDbContext>();

var origins = dbContext.Clients.SelectMany(x => x.AllowedCorsOrigins.Select(y => y.Origin)).ToList();

var distinctOrigins = origins.Where(x => x != null).Distinct();

var isAllowed = distinctOrigins.Contains(origin, StringComparer.OrdinalIgnoreCase);

_logger.LogDebug("Origin {origin} is allowed: {originAllowed}", origin, isAllowed);

return Task.FromResult(isAllowed);
}

查看 isAllowed,它填充了来自 AllowedCrossOrigins 集合的数据,其内容存储在 [dbo].[ClientCorsOrigin] 表中。

因此,请仔细检查 ClientCorsOrigin 表中的内容。

关于c# - 身份服务器 4 - 配置了允许的来源但不允许来源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51326178/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com