飞行前访问控制允许凭据
Preflight Access-Control-Allow-Credentials
我的 ASP.NET 部署为 Azure 应用服务的核心 Web 应用遇到 CORS 问题。前端部署为不同的应用程序服务。当我尝试从前端访问 Api 时,出现以下错误。
Failed to load
https://chatclassifier.azurewebsites.net/chatcontroller/getchatData:
Response to preflight request doesn't pass access control check: The
value of the 'Access-Control-Allow-Credentials' header in the response
is '' which must be 'true' when the request's credentials mode is
'include'. Origin
'https://chatclassifierangularfrontend20180924084838.azurewebsites.net'
is therefore not allowed access. The credentials mode of requests
initiated by the XMLHttpRequest is controlled by the withCredentials
attribute.
我正在使用一些 cookie 在客户端和服务器之间传递数据。
我在ASP.NET中设置了更多的CORS策略,在Setup.cs
中如下核心
配置服务:
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
{
options.AddPolicy("AllowAllHeaders",
builder =>
{
builder
.WithOrigins("https://XXXXXXXXXXangularfrontend20180924084838.azurewebsites.net")
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
});
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.Configure<FormOptions>(x =>
{
x.ValueLengthLimit = int.MaxValue;
x.MultipartBodyLengthLimit = int.MaxValue;
});
}
配置:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
var cookiePolicyOptions = new CookiePolicyOptions
{
Secure = CookieSecurePolicy.SameAsRequest,
MinimumSameSitePolicy = SameSiteMode.None
};
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
}
app.UseCors("AllowAllHeaders");
app.UseCookiePolicy(cookiePolicyOptions);
app.UseHttpsRedirection();
app.UseMiddleware(typeof(ApiExceptionHandling));
app.UseMvc();
}
我没有在我的 API 代码中的其他任何地方添加任何其他 CORS 相关代码。我尝试将 CORS "AllowAllHeaders" 属性添加到我的控制器,但这也没有用。
此外,我还在 CORS 下的 Azure 网络应用程序配置中添加了以下代码行。
显然我已经更改了 URL 名称以包含 XXXXXX 并确保它们在我的 Azure 门户和 Web 应用程序代码中是相同的。
如有任何帮助,我们将不胜感激!
您不应将 Azure 应用服务 CORS 配置与代码中的配置结合使用。如果你在应用服务上开启了CORS,那么你的CORS配置在代码中基本上会被忽略。
这是因为应用服务拦截了 OPTIONS 请求。
我的 ASP.NET 部署为 Azure 应用服务的核心 Web 应用遇到 CORS 问题。前端部署为不同的应用程序服务。当我尝试从前端访问 Api 时,出现以下错误。
Failed to load https://chatclassifier.azurewebsites.net/chatcontroller/getchatData: Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Credentials' header in the response is '' which must be 'true' when the request's credentials mode is 'include'. Origin 'https://chatclassifierangularfrontend20180924084838.azurewebsites.net' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
我正在使用一些 cookie 在客户端和服务器之间传递数据。
我在ASP.NET中设置了更多的CORS策略,在Setup.cs
中如下核心配置服务:
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
{
options.AddPolicy("AllowAllHeaders",
builder =>
{
builder
.WithOrigins("https://XXXXXXXXXXangularfrontend20180924084838.azurewebsites.net")
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
});
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.Configure<FormOptions>(x =>
{
x.ValueLengthLimit = int.MaxValue;
x.MultipartBodyLengthLimit = int.MaxValue;
});
}
配置:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
var cookiePolicyOptions = new CookiePolicyOptions
{
Secure = CookieSecurePolicy.SameAsRequest,
MinimumSameSitePolicy = SameSiteMode.None
};
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
}
app.UseCors("AllowAllHeaders");
app.UseCookiePolicy(cookiePolicyOptions);
app.UseHttpsRedirection();
app.UseMiddleware(typeof(ApiExceptionHandling));
app.UseMvc();
}
我没有在我的 API 代码中的其他任何地方添加任何其他 CORS 相关代码。我尝试将 CORS "AllowAllHeaders" 属性添加到我的控制器,但这也没有用。
此外,我还在 CORS 下的 Azure 网络应用程序配置中添加了以下代码行。
显然我已经更改了 URL 名称以包含 XXXXXX 并确保它们在我的 Azure 门户和 Web 应用程序代码中是相同的。
如有任何帮助,我们将不胜感激!
您不应将 Azure 应用服务 CORS 配置与代码中的配置结合使用。如果你在应用服务上开启了CORS,那么你的CORS配置在代码中基本上会被忽略。
这是因为应用服务拦截了 OPTIONS 请求。