![](/img/trans.png)
[英]Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Credentials' header in the response is ''
[英]Preflight Access-Control-Allow-Credentials
我將ASP.NET Core Web應用程序部署為Azure應用程序服務時遇到了CORS問題。 前端被部署為其他應用程序服務。 當我嘗試從前端訪問Api時,出現以下錯誤。
無法加載https://chatclassifier.azurewebsites.net/chatcontroller/getchatData :對預檢請求的響應未通過訪問控制檢查:響應中“ Access-Control-Allow-Credentials”標頭的值為“當請求的憑據模式為“包含”時,必須為“ true”。 因此,不允許訪問來源' https://chatclassifierangularfrontend20180924084838.azurewebsites.net '。 XMLHttpRequest發起的請求的憑據模式由withCredentials屬性控制。
我正在使用一些cookie在客戶端和服務器之間傳遞數據。
我在Setup.cs中如下在ASP.NET Core中設置了更多CORS策略
配置服務:
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中將以下代碼行添加到我的Web應用程序配置中。
顯然,我已將URL名稱更改為包括XXXXXX,並確保它們在我的Azure門戶和Web應用程序代碼中都相同。
任何幫助將不勝感激!
您不應將Azure App Service CORS配置與代碼中的配置結合使用。 如果在應用程序服務上啟用CORS,則在代碼中基本上會忽略您的CORS配置。
這是由於App Service攔截了OPTIONS請求。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.