cookies没有被设置为服务器和客户端应用之间的生产

发布于 2025-02-09 04:10:57 字数 1395 浏览 1 评论 0原文

我在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文