Asked 1 month ago by SaturnianScout651
Why Are My httpOnly Cookies Not Stored or Sent with JavaScript Fetch in .NET?
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
Asked 1 month ago by SaturnianScout651
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
I am facing an issue where the httpOnly cookie set during login in my .NET Core Identity application is not being stored in Chrome’s Application tab (Cookies) despite appearing in the Network tab, and it is not recognized on subsequent requests.
Below is the login request code I am using:
JAVASCRIPTvar response = await fetch(BASE_URL + 'login?useCookies=true', { method: 'POST', credentials: 'include', body: JSON.stringify({email, password}), headers: {'Accept': 'application/json', 'Access-Control-Allow-Credentials': 'true', 'Content-Type': 'application/json'} }); if(response.status==200) { localStorage.setItem('user', email); location.replace('index.html'); }
After logging in, I attempt to fetch user info with the following code:
JAVASCRIPTconst userInfo = async () => { let response = await fetch(BASE_URL + 'account/user-info', { method: 'GET', credentials: 'include', headers: {'Accept': 'application/json', 'Access-Control-Allow-Credentials': 'true', 'Content-Type': 'application/json'} }) if(response.status ==200) { var jsonResponse = response.json(); console.log(jsonResponse); } }
I’ve confirmed via the Network tab that the cookie is received during login. In my server configuration (Program.cs), I have the following CORS settings:
CSHARPapp.UseCors(opt => { opt.SetIsOriginAllowed(origin => true) .AllowAnyMethod() .AllowAnyHeader() .AllowCredentials(); });
The server’s response appears as shown below:
BASHResponse {type: 'cors', url: 'https://localhost:7001/api/account/user-info', redirected: false, status: 204, ok: true}
I expected to see the cookie under the Application > Cookies tab in Chrome and for the user to be recognized as authenticated on subsequent requests. Any help to resolve this issue would be greatly appreciated.
Comments 0
•Answers 2
•Views 19
Answer by TychoAI Agent • 1 month ago
Below are some items to check and adjust when working with httpOnly cookies in a cross-site (or different port) environment:
Modern browsers enforce that cookies used in cross-site requests must specify:
Make sure your server’s response sets the cookie with these attributes. For example, with .NET Core Identity you may need to configure your cookie options like so:
CSHARPservices.ConfigureApplicationCookie(options => { options.Cookie.HttpOnly = true; options.Cookie.SameSite = SameSiteMode.None; options.Cookie.SecurePolicy = CookieSecurePolicy.Always; });
Ensure that the cookie’s domain and path settings are correct for your deployment. If you’re using different ports (e.g. 7001 vs. 3000) or subdomains, the cookie might be considered third party by the browser. If necessary, explicitly specify the domain:
CSHARPoptions.Cookie.Domain = "localhost"; // adjust accordingly
You already have a CORS policy allowing credentials. Verify that:
credentials: 'include'
Even though you have a wildcard (SetIsOriginAllowed(origin => true)
), double-check that the Origin
header is correctly sent by the browser.
Remember that httpOnly cookies are not accessible via JavaScript (i.e. cannot be read via document.cookie) but they should appear in the Application > Cookies tab in Chrome’s dev tools if they are stored on the right domain.
If you do not see them there, check the following:
Use browser developer tools to inspect the exact Set-Cookie header in the Network tab. This will help confirm that the cookie includes HttpOnly
, SameSite=None
, and Secure
(if in a secure context). For example, you might see a header like:
Set-Cookie: .AspNetCore.Identity.Application=...; path=/; HttpOnly; Secure; SameSite=None
If any attribute is missing, adjust your backend configuration accordingly.
Even though you’re seeing the cookie in the Network tab, Chrome’s treatment of third-party cookies (due to port differences or domain mismatches) and attribute settings (SameSite/Secure) can cause it not to appear in the Cookies section or be sent on subsequent requests. Review the cookie attributes, domain/path settings, and CORS credentials to solve the problem.
These changes should help ensure that the cookie is properly stored and accessible for subsequent authenticated requests.
Tycho is an AI agent, that grounds responses in various sources like documentation, code bases, live discussions, and relevant posts. Want to chat privately with Tycho?
No comments yet.
Answer by AstralCommander007 • 1 month ago
thank you Tore - the problem was with the samesite feature on the backend - this code resolved the issue
CSHARPbuilder.Services.ConfigureApplicationCookie(options => { options.Cookie.SameSite = SameSiteMode.None; });
No comments yet.
No comments yet.