[英]ASP.NET Core webpage ignores CORS policy in Startup.cs
這不是之前問題的重復,因為他們的解決方案說要添加 CORS 策略。 我的問題是,盡管存在 CORS 政策,但為什么在這種情況下會忽略該政策。
我正在創建一個基本的 ASP.NET Core 2.1 網頁,它將與在localhost:5082
運行的 docker 容器進行通信。 (是的,.NET Core 2.1 有點老了,但出於部門類型的原因,它目前對我們來說是必要的邪惡。)
因此,很自然地,當嘗試通過 Visual Studio 調試器運行該網頁項目時,由於 CORS 策略,嘗試訪問該容器時頁面上的 AJAX 請求失敗。 所以...我已經進入Startup.cs
並添加了一個非常自由的 CORS 策略,但出於某種原因,它被忽略了。
我已經嘗試了幾個類似的帖子和 SO 答案,它們都在做非常相似的事情,但這是我的 Startup.cs 可能是什么樣子的一個例子:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace TheNamespace
{
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.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddCors(options =>
{
options.AddPolicy("AllowAll",
builder =>
{
builder
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
});
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
}
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseCors("AllowAll");
app.UseMvc();
}
}
}
注意對services.AddCors
和app.UseCors
的調用。 不同的帖子和 SO 答案對此的表達略有不同,但它們都指向與此非常相似的Startup.cs
。
但這不知何故被忽略了,我對http://localhost:5082/...
非常基本的 JavaScript GET
調用仍然失敗並顯示以下錯誤消息:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:5082/... (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).
怎么了?
這是失敗的 JavaScript( url
是一個字符串值):
var xmlHttp = new XMLHttpRequest();
xmlHttp.open('GET', url, false);
//xmlHttp.setRequestHeader("Access-Control-Allow-Origin", "*");
xmlHttp.send(null);
return xmlHttp.responseText;
編輯:
這不是一個 MVC 項目或任何花哨的東西。 創建項目時,我只選擇了Web Application
選項。 所以現在,它沒有控制器,只有頁面。
編輯:
根據評論,這基本上是我的項目結構:
Pages:
Shared:
_CookieConsentPartial.cshtml
_Layout.cshtml
_ValidationScriptsPartial.cshtml
_ViewImports.cshtml
_ViewStart.cshtml
Index.cshtml
Program.cs
Startup.cs
就 URL 而言,它是http://localhost:5082/api/buildcactus
。
這對我有用。 builder 中的CorsPolicyBuilder
配置策略。 現在你可以在控制器中使用它:
[EnableCors("AllowAll")]
public class MyController
{
}
app.UseCors("AllowAll")
單獨可能不適用於 API 端點。 嘗試在控制器上顯式添加[EnableCors("AllowAll")]
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.