我想讓CORS使用新的WebAPI項目。該項目只是使用ActiveDirectoryBearerAuthentication
的默認WebAPI模板(即具有MVC和WebAPI參考)。使用WebAPI啓用CORS
每當我試圖讓我的API請求,我遇到了以下錯誤:
XMLHttpRequest cannot load https://localhost:44385/api/values. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://localhost:44369' is therefore not allowed access.
我已經安裝Microsoft.Owin.Cors和Microsoft.AspNet.WebApi.Cors。
我WebApiConfig是這樣的:
public static void Register(HttpConfiguration config)
{
config.EnableCors();
....
我Startup.Auth是這樣的:周圍的淨
public void ConfigureAuth(IAppBuilder app)
{
app.UseCors(CorsOptions.AllowAll);
app.UseWindowsAzureActiveDirectoryBearerAuthentication(
....
解決方案說補充:
<customHeaders>
<!-- Adding the following custom HttpHeader will help prevent CORS from stopping the Request-->
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
到web.config 。但是,這樣做只是允許所有內容,而不管Microsoft.AspNet.WebApi.Cors
是否已安裝,啓用或不可用。
難道你不希望你允許一切,或者你仍然想限制訪問和阻止一些其他請求 –
有一點要知道的是,IE需要在響應中包含P3P頭以便CORS工作 – pm100
我想阻止除一個域之外的所有內容。我以爲我會倒退,以確保它正在工作。如果我沒有做到這一點,我會認爲web.config解決方案解決了它 - 它不是隻是允許所有的東西,無論CORS配置。 – Mardoxx