cookies没有被设置为服务器和客户端应用之间的生产
我在Heroku上使用Postgres数据库托管了.NET 6中的Web API。我也有一个在Firebase上举办的Angular 13的前端。我的CORS策略配置得很好,并且我以前对CORS有问题,但在Localhost中。经过一些问题,我找到了一个解决方案,并且仅在Localhost(不同的端口)设置了Cookie。但是,由于某种原因,我的饼干没有被固定在生产中,但是我可以看到我正在收到Angular应用中的set-cookie标头。那么,怎么了?
更新i
后端的CORS政策
// CorsConfig:WebUrl returns the URL of my angular app.
builder.Services.AddCors(x => {
x.AddPolicy("WebPolicy", opt => {
opt.WithOrigins(builder.Configuration.GetSection("CorsConfig:WebUrl").Value)
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials();
});
});
var app = builder.Build();
app.UseRouting();
app.UseHttpsRedirection();
app.UseCors("WebPolicy");
我如何设置Cookie
HttpContext.Response.Cookies.Append("d_a", a, new CookieOptions() { Secure = true, SameSite = SameSiteMode.None });
HttpContext.Response.Cookies.Append("d_r", r, new CookieOptions() { Secure = true, SameSite = SameSiteMode.None, HttpOnly = true });
请求登录和在Angular App中获取cookie
signIn(user : any) : Observable<any> {
return this.httpClient.post(this.apiUrl + "login", user, withBodyHttpOptions);
export const withBodyHttpOptions = {
headers: new HttpHeaders({'Content-Type': 'application/json'}),
withCredentials: true
}
}
I have a web API in .NET 6 hosted on heroku with a postgres database. I also have a front end in angular 13 hosted on firebase. My CORS policy is all well configured, and I had problem with CORS before, but in localhost. After some problems, I found a solution, and the cookies was being set only in localhost (different ports). But, for some reason, my cookies are not being set in production, but I can see that I'm receiving the SET-COOKIE headers in my angular app. So, what is wrong?
Update I
CORS Policy in Backend
// CorsConfig:WebUrl returns the URL of my angular app.
builder.Services.AddCors(x => {
x.AddPolicy("WebPolicy", opt => {
opt.WithOrigins(builder.Configuration.GetSection("CorsConfig:WebUrl").Value)
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials();
});
});
var app = builder.Build();
app.UseRouting();
app.UseHttpsRedirection();
app.UseCors("WebPolicy");
How I'm setting the cookies
HttpContext.Response.Cookies.Append("d_a", a, new CookieOptions() { Secure = true, SameSite = SameSiteMode.None });
HttpContext.Response.Cookies.Append("d_r", r, new CookieOptions() { Secure = true, SameSite = SameSiteMode.None, HttpOnly = true });
Request to login and get cookies in angular app
signIn(user : any) : Observable<any> {
return this.httpClient.post(this.apiUrl + "login", user, withBodyHttpOptions);
export const withBodyHttpOptions = {
headers: new HttpHeaders({'Content-Type': 'application/json'}),
withCredentials: true
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论