I created the project using visual studio 2022 and chose aspnet core 6 web api template. I'm trying to set a cookie in the browser but it seems I am missing something as it is not setting anything under Network > Application > Cookies
My frontend is a react app. No extra library just yet. It's the default project after running the npx create-react-app <project-name>
command.
I can call the /weatherforecast
endpoint no problem. But for some reason, it is not setting the cookie.
frontend call
const getData = async () => {
await axios.get("/weatherforecast");
};
WeatherForecastController.cs
public IActionResult Get()
{
Response.Cookies.Append("myjwt", "ABCDE", new CookieOptions
{
Secure = true,
HttpOnly = true,
SameSite = SameSiteMode.None
});
return Ok();
}
Program.cs
var builder = WebApplication.CreateBuilder(args);
const string AllowAllHeadersPolicy = "AllowAllPolicy";
builder.Services.AddCors(options =>
{
options.AddPolicy(AllowAllHeadersPolicy,
builder =>
{
builder
.WithOrigins(new[] { "http://localhost:3000" })
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
});
});
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
app.UseCors(AllowAllHeadersPolicy);
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
The end goal of what I am trying to do is to store the refresh token in the cookie with the HttpOnly=true
What I tried:
- I am using insomnia to test the endpoint. Working perfectly! Setting the cookie too.
- Found this question but no clear answer.