I can't save a cookie or get any headers from asp.net web api using CORS. But in postman I can see every header or cookie set in response.
My frontend request:
var response = await fetch("https://localhost:7072/api/Auth/Register", {
method: "post",
mode: "cors",
credentials: "same-origin",
headers: {
"Origin": "https://localhost:3000"
},
body: new FormData(document.getElementById("myForm"))
});
response.headers.forEach((val, key) => console.log(key + ": " + val));
I also tried credentials: include but I cant get the headers anyway
But in response headers I get only content-length: 0 and no cookie set.
My backend policy is:
builder.Services.AddCors(options =>
{
options.AddPolicy("CorsPolicy", builder =>
{
builder.WithOrigins("https://localhost:3000")
.AllowAnyHeader().
AllowCredentials().
AllowAnyMethod();
});
});
Build:
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseCors("CorsPolicy");
app.UseAuthentication();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller}/{action=Index}/{id?}");
app.MapFallbackToFile("index.html"); ;
app.Run();
And Auth/Register looks like:
var token = GenerateToken(user);
Response.Cookies.Append("LOGIN_INFO", token, new CookieOptions
{
Expires = DateTime.Now.AddMonths(3),
HttpOnly = true,
Secure = true
});
return Ok();
I tried using SameSite.None, Lax etc.