[英]C# The 'Access-Control-Allow-Credentials' header in the response is '' which must be 'true' when the request's credentials mode is 'include'
[英]The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'
我的應用程序在IE瀏覽器中運行良好, 但由於CORS
問題 , 它無法在Chrome瀏覽器中運行。
問題是
無法加載http:// localhost:52487 / api / Authentication / :當請求的憑據模式為“include”時,響應中的“Access-Control-Allow-Origin”標頭的值不能是通配符“*” 。 因此不允許來源' http:// localhost:4200 '訪問。 XMLHttpRequest發起的請求的憑據模式由withCredentials屬性控制。
我在前端使用angular 2並在后端使用Asp.net core 1.0。 我試過了
這是我的啟動代碼
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
{
options.AddPolicy("AllowAll", p =>
{
p.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod();
});
});
// Add framework services.
services.AddMvc();
// Add functionality to inject IOptions<T>
services.AddOptions();
// Add our Config object so it can be injected
services.Configure<Data>(Configuration.GetSection("Data"));
services.Configure<COCSettings>(Configuration.GetSection("COCSettings"));
services.Configure<EmailSettings>(Configuration.GetSection("EmailSettings"));
AppSettings.ConnectionString = Configuration["Data:DefaultConnectionString"];
// *If* you need access to generic IConfiguration this is **required**
services.AddSingleton<IConfiguration>(Configuration);
// Injecting repopsitories with interface
AddServices(services);
// Add Json options
services.AddMvc().AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseMiddleware(typeof(ErrorHandling));
app.UseMiddleware(typeof(GetNoCache));
app.UseCors("AllowAll");
app.UseMvc();
}
這就是我從UI(角度)方面調用API的方式
constructor(private http: Http) {
this.headers = new Headers();
this.headers.append('Accept', 'application/json');
}
GetMaintainCOC(FYONId) {
return this.http.get(this.apiUrl + 'GetCertificationofConformity?FYONId=' + FYONId, { withCredentials: true })
.map(responce => <any>responce.json())
.catch(error => {
return Observable.throw(error);
});
}
這是工作,當我打電話AllowCredentials()
內AddPolicy
services.AddCors(options =>
{
options.AddPolicy("AllowAll", p =>
{
p.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials();
});
});
我從Access-Control-Allow-Origin獲得了這個想法的關鍵:當credentials標志為true時,不允許使用“*”,但是沒有Access-Control-Allow-Credentials標頭
我在角度
http
服務調用中使用{ withCredentials: true }
。 所以我想我應該在CORS
服務中使用AllowCredentials()
策略。
好吧,你似乎已經解決了,但這是簡單的答案。
如果在請求定義中設置withCredentials
標志,則會在請求中傳遞cookie等。 否則他們將不會通過。
如果您的服務器返回任何Set-Cookie
響應頭,那么您還必須返回Access-Control-Allow-Credentials: true
響應頭,否則將不會在客戶端上創建 cookie。 如果你這樣做,你還需要指定的確切來源Access-Control-Allow-Origin
響應頭,因為Access-Control-Allow-Origin: *
是不兼容的憑據。
這樣做:
Access-Control-Allow-Origin: <value-of-Origin-request-header>
響應頭 Access-Control-Allow-Credentials: true
響應頭
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.