C
C#4w ago
Vandan

Cookies and JWT for Authentication

I'm working on a project and need help identifying an authentication issue I'm facing. My project uses ASP.NET Core 9 for the backend and React for the frontend. I recently converted JWT authentication to use cookies, but now authentication is not working. The claims properties are missing, and the user is not getting authenticated. If anyone has experience with this, please help. I Update the ProgramFile as well
builder.Services.AddAuthentication(options => { options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme; options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme; options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }) .AddCookie(options => { options.Cookie.Name = ".AspNetCore.Cookies"; // Ensure this matches the actual cookie name options.Cookie.HttpOnly = true; options.Cookie.SecurePolicy = CookieSecurePolicy.Always; // Set to None if testing locally without HTTPS options.Cookie.SameSite = SameSiteMode.Strict; options.Cookie.IsEssential = true; options.LoginPath = "/auth/login"; // Adjust as needed options.LogoutPath = "/auth/logout"; }) .AddJwtBearer(options => { options.RequireHttpsMetadata = false; options.SaveToken = true; options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuerSigningKey = true, IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("qwertyQWERTY12345ASDFzxcv67890mnbLKj0i")), // Ensure this matches JWT secret ValidateIssuer = false, ValidateAudience = false, ValidateLifetime = true, ClockSkew = TimeSpan.Zero }; })
3 Replies
Vandan
VandanOP4w ago
var cookieOptions = new CookieOptions { HttpOnly = true, Secure = true, // Set to false if testing locally SameSite = SameSiteMode.None, Expires = DateTime.UtcNow.AddHours(1) }; Response.Cookies.Append("accessAuthToken", accessToken, cookieOptions); Response.Cookies.Append("refreshAuthToken", refreshToken, cookieOptions); I used this in Controller
Unknown User
Unknown User4w ago
Message Not Public
Sign In & Join Server To View
Vandan
VandanOP4w ago
Thank you

Did you find this page helpful?