[SOLVED] Accessing HttpOnly Cookies during Prerendering
When using a Blazor Web app (WASM) with server-side prerendering, the page initially loads on the server and then on the client. During the prerendering phase, I can send a request to my API to retrieve some data that is authenticated using an access token that was stored in the cookie. But the HttpContext does not seem to have any cookies during prerendering. How can I access the cookies in this situation?
21 Replies
You're right — during the prerendering phase in Blazor WASM with server-side rendering, the component is rendered on the server, but the usual
HttpContext
isn't directly available in Blazor components.
To access cookies (including auth tokens) during prerendering, you can:
1. Inject IHttpContextAccessor in a service used only on the server side.
2. Access the cookie via:
var token = httpContextAccessor.HttpContext?.Request.Cookies["YourCookieName"];
But be careful — this only works during prerendering on the server.
3. Make sure the service accessing the cookie is marked with [CascadingParameter]
or injected differently between server/client, because once it switches to WASM, there’s no HttpContext
.
Also, be sure that the cookie isn’t marked as HttpOnly
if you need to read it in JavaScript (like when fetching data client-side after hydration).people are capable of using AI themselves. pasting results from them and passing them off as your own is unhelpful
So are you a developer?
@tsuyoshi 羽鳥 Yeah, please no AI answers. Thanks.
I think so
Where are you from?
Why do you want to know where I am from?
i'd just ignore them. pretty sure they're just here to get recruited
as for your question, i haven't used blazor wasm, but there are a couple possibilities
1. how are you obtaining the HttpContext
2. is the cookie on the same origin as the server serving the blazor application?
1. During a request to an Endpoint, you get access to the HttpContext
2. Yes
oh this is in the endpoint
if you check f12 is the cookie present in the API request?
Do you mean f12 in the browser for devtools?
yeah
The issue is happening during prerendering
Anyways, yes the cookies are there

not sure unfortunately
No worries, this has been my entire experience with Blazor. There always seems to be some kind of workaround needed to get it to function properly. Sigh.
yeahhhh, i ended up abandoning blazor for a react + web api combo
I probably would have done the same if I didn't despise JavaScript
with TS it's not so bad
Yeah it probably is, but I think I will stick with Blazor 😅
godspeed
I found the solution on Reddit. I just needed to manually add the cookie from the HttpContext to the HttpClient Header before making the endpoint request.
https://www.reddit.com/r/Blazor/comments/1k2asvl/comment/mnw30rr/?utm_source=share&utm_medium=web3x&utm_name=web3xcss&utm_term=1&utm_content=share_button
Reddit
SchlaWiener4711's comment on "How to Access HttpOnly Cookies during...
Explore this conversation and more from the Blazor community