When login user, a cookie is created and sent to the browser.
In client browser, in the Inspect section, in the Network tab, there is cookie both in the Headers tab, in the ResponseHeaders section marked with the Set-Cookie key, and in the Cookies tab.
But the cookie is not set in the application tab and in the storage section, and as a result, the cookie is not stored in the browser.
at frontend(React.js), can not read Set-Cookie in JavaScript.
I wanted to know if it is possible to send a cookie to the browser with another header or custom header?
Because I receive all ResponseHeaders except Set-Cookie.
Or can a cookie be set to be saved as soon as it reaches the browser?
I use Identity for login user.
my Program.cs:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddHttpContextAccessor();
builder.Services.Configure<CookieTempDataProviderOptions>(options => { options.Cookie.IsEssential = true; });
builder.Services.Configure<CookiePolicyOptions>(options =>
{
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, x =>
{
x.Cookie.Name = "mycookie";
x.Cookie.HttpOnly = true;
});
builder.Services.AddCors(options => options.AddPolicy("MyPolicy", build =>
build.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod().WithExposedHeaders("*")));
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
}
app.UseCors("MyPolicy");
app.UseAuthentication();
app.UseAuthorization();
app.UseHttpsRedirection();
app.UseRouting();
app.UseStaticFiles();
app.UseCookiePolicy(
new CookiePolicyOptions
{
Secure = CookieSecurePolicy.Always
});
app.MapControllers();
app.Run();
my login method:
public void Signin(UserViewModel user)
{
var claims = new List<Claim>
{
new Claim("AccountId", user.Id.ToString()),
new Claim(ClaimTypes.Name, user.Name),
};
var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
var authProperties = new AuthenticationProperties
{
ExpiresUtc = DateTimeOffset.UtcNow.AddDays(1)
};
_contextAccessor.HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,
new ClaimsPrincipal(claimsIdentity),
authProperties);
}
Thanks for those help me!