【发布时间】:2022-09-23 09:39:55
【问题描述】:
我开始了一个 .NET CORE 5 项目
我选择了windows类型认证
这种类型的项目在客户端给我一个CORS错误(反应)
但是,如果我没有选择 windows 而不是 windows,我将不会收到错误
这是来自客户端的调用:
const res = await fetch(`https://localhost:44373/weatherforecast`)
我需要这种类型的项目,因为我想使用 AD 身份验证
我尝试将其添加到 fetch 调用中:
const res = await fetch(`https://localhost:44300/weatherforecast`,{credentials: \'include\'})
并更改启动:
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace WebApplication3
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to
add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddCors(options =>
{
options.AddPolicy(\"MyMyAllowCredentialsPolicy\",
policy =>
{
policy.WithOrigins(\"https://localhost:44300\")
.AllowCredentials();
});
});
}
// This method gets called by the runtime. Use this method to
configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app,
IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseCors();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}
-
恐怕你可以在你的 fetch 方法中尝试include the credential -->
fetch(\'https://xxx\', {credentials: \'include\'})和 enable credential 在你的 api 中 --->policy.WithOrigins(\"http://example.com\").AllowCredentials(); -
它对我不起作用,我将 STARTUP 文件附加到问题中
-
您是否使用 .AllowAnyOrigin() 方法进行了测试?我想这会工作
-
查了一下,改成小王说的了。。
-
起初我认为 Cors 问题可能与 cors 策略有关,但正如您提到的禁用 Windows 身份验证将使其工作,所以我认为您已经设置了 cors 策略,所以我推断它可能与
AllowCredentials()有关。因为它仍然无法工作。我发现this question 看起来和你的场景很相似,你能看看吗?
标签: asp.net-core windows-authentication webapi