我有兩個應用程序:.NET Core 2.0 中的 WebAPI.NET Core 2.0 中的前端 MVC,它使用 javascript 發(fā)送 post 請求。在兩個初創(chuàng)公司中,我都有ConfigureServicesservices.AddCors();services .AddMvc() .AddFluentValidation(fv => fv.RegisterValidatorsFromAssemblyContaining<Startup>());Configureapp.UseDeveloperExceptionPage();app.UseAuthentication();app.UseCors(builder => builder .AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader() .AllowCredentials() );app.UseMvc();但仍然在瀏覽器的控制臺中顯示“同源策略”不允許您從“ http://10.1.5.6:12345/test/add ”加載遠程資源。(缺少 CORS 標題“訪問控制允許來源”)
2 回答

慕碼人8056858
TA貢獻1803條經驗 獲得超6個贊
請拋出你必須得到答案 [Cors .net core 的完整詳細信息]
https://docs.microsoft.com/en-us/aspnet/core/security/cors?view=aspnetcore-2.1
在你的情況下
options.AddPolicy("AllowAllMethods",
builder =>
{
builder.WithOrigins("http://example.com")
.AllowAnyMethod();
});

眼眸繁星
TA貢獻1873條經驗 獲得超9個贊
當您指定 .AllowCredentials() 時,您需要添加 .WithOrigins(" http://frontenddomain ")。這將在 Access-Control-Allow-Origin 標頭中添加特定域。
否則刪除 .AllowCredentials() 然后 AllowAnyOrigin() 將起作用,因為這將在標題中添加 * 。
瀏覽器不允許憑據傳遞帶有 * origin 的 auth cookie。因此,根據您的用例,要么使用 AllowCredentials + WithOrigins,要么僅使用 AllowAnyOrigin
- 2 回答
- 0 關注
- 195 瀏覽
添加回答
舉報
0/150
提交
取消