Angular 应用程序 运行 在不同端口上调用 dotnet 应用程序的本地主机在降级时得到 no-referrer-when-downgrade。由于 withCredentials?

Angular app running localhost calling dotnet app on different port gets no-referrer-when-downgrade. Due to withCredentials?

我使用 Angular CLI 设置了一个新的 Angular 应用程序,它在 http://localhost:4200 上运行 我调用我使用在 http://localhost:5000

上运行的 dotnet 核心开发的 Web 服务

我允许 CORS 用于本地主机设置。我确信它在过去有效,但现在我得到

no-referrer-when-downgrade

chrome 中的错误消息。

这在某种程度上与 withCredentials: true 有关 如果我输入 false 那么它就可以正常工作。

如何将 http 调用的凭据传递到同一本地主机域上的不同端口?

您可以尝试将 .NET Core 应用程序升级到 HTTPS。

no-referrer-when-downgrade 消息表明默认引荐来源策略已到位,如 here 所述。

no-referrer-when-downgrade (default)

This is the default behavior if no policy is specified, or if the provided value is invalid. The origin, path, and querystring of the URL are sent as a referrer when the protocol security level stays the same (HTTP→HTTP, HTTPS→HTTPS) or improves (HTTP→HTTPS), but isn't sent to less secure destinations (HTTPS→HTTP).

只是为了测试,您可以尝试将开发环境的引用策略设置为不同的值(此代码使用 NWebsec.AspNetCore.Middleware):

if (env.IsDevelopment())
{
    app.UseReferrerPolicy(opts => opts.UnsafeUrl());
}

原来这不是降级时没有推荐人的问题,而是 CORS 问题。 我在 CORS 策略中缺少 AllowCredentials()

    public void ConfigureServices(IServiceCollection services)
    {
        // add cors
        services.AddCors(options =>
        {
            options.AddPolicy(name: "MyCorsPolicy",
                builder => builder.SetIsOriginAllowed(s => s.Contains("localhost"))
                    .AllowAnyMethod()
                    .AllowAnyHeader()
                    .AllowCredentials());
        });
    }