C
C#4d ago
TJ3046

[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
tsuyoshi 羽鳥
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).
mg
mg3d ago
people are capable of using AI themselves. pasting results from them and passing them off as your own is unhelpful
tsuyoshi 羽鳥
So are you a developer?
TJ3046
TJ3046OP3d ago
@tsuyoshi 羽鳥 Yeah, please no AI answers. Thanks.
tsuyoshi 羽鳥
I think so Where are you from?
TJ3046
TJ3046OP3d ago
Why do you want to know where I am from?
mg
mg3d ago
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?
TJ3046
TJ3046OP3d ago
1. During a request to an Endpoint, you get access to the HttpContext 2. Yes
mg
mg3d ago
oh this is in the endpoint if you check f12 is the cookie present in the API request?
TJ3046
TJ3046OP3d ago
Do you mean f12 in the browser for devtools?
mg
mg3d ago
yeah
TJ3046
TJ3046OP3d ago
The issue is happening during prerendering
TJ3046
TJ3046OP3d ago
Anyways, yes the cookies are there
No description
mg
mg3d ago
not sure unfortunately
TJ3046
TJ3046OP3d ago
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.
mg
mg3d ago
yeahhhh, i ended up abandoning blazor for a react + web api combo
TJ3046
TJ3046OP3d ago
I probably would have done the same if I didn't despise JavaScript
mg
mg3d ago
with TS it's not so bad
TJ3046
TJ3046OP3d ago
Yeah it probably is, but I think I will stick with Blazor 😅
mg
mg3d ago
godspeed
TJ3046
TJ3046OP3d ago
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

Did you find this page helpful?