Skip to content

Update section on passing tokens in Blazor Web Apps #31691

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
guardrex opened this issue Feb 5, 2024 · 22 comments · May be fixed by #35482
Open

Update section on passing tokens in Blazor Web Apps #31691

guardrex opened this issue Feb 5, 2024 · 22 comments · May be fixed by #35482

Comments

@guardrex
Copy link
Collaborator

guardrex commented Feb 5, 2024

Description

Per our offline discussion, I'll see about implementing Halter's suggestion at Stephen's remarks fleshed out further for this.

Also see #35225.

Here's the LIVE section link that merely tells readers that this content is due to be updated ...

https://learn.microsoft.com/en-us/aspnet/core/blazor/security/server/additional-scenarios?view=aspnetcore-8.0#pass-tokens-to-a-server-side-blazor-app

What we have for Blazor Server is in the 7.0 version of the article ...

https://learn.microsoft.com/en-us/aspnet/core/blazor/security/server/additional-scenarios?view=aspnetcore-7.0#pass-tokens-to-a-server-side-blazor-app

Page URL

https://learn.microsoft.com/en-us/aspnet/core/blazor/security/server/additional-scenarios?view=aspnetcore-8.0

Content source URL

https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/blazor/security/server/additional-scenarios.md

Document ID

c98be365-408d-7ee6-cb74-14c44d01b0b8

Article author

@guardrex

@timohermans
Copy link

timohermans commented Feb 6, 2024

I just found this issue, so I'll just leave my two cents here.

I tried applying this to my own blazor server 8 app. When adding a scoped TokenProvider service, the set value will not be handed over to the circuit. The value will initially be set during the prerender, but on the second oninitializedasync null.

What did work was marking the service as singleton, though I'm not sure if this is the way the documentation intended it to be done.

@GStoynev
Copy link

#31759 got closed, but let me share what worked for me, if helpful to patch the documentation or to others searching for solution to the issue:

Capturing a cookie inline like this and then passing it as a Parameter to the Routes component where it's stored in the scoped provider seems to do the trick:

<body>
@{
    var cookie = HttpContext?.Request.Cookies["UI"];
    Logger.LogInformation("Cookie captured: {UICookie}", cookie);
}

<Routes Cookie="@cookie" @rendermode="InteractiveServer"/>
<script src="_framework/blazor.web.js"></script>

</body>

@GStoynev
Copy link

Another suggestion for that section of the documentation:

Mixing OpenIdConnect in the example creates the impression that there might be some magic happening there, that alleviates the issue with the pre-rendering and the duplicated TokenProvider.

A simple example with a cookie would have been simpler and to the point.

@timohermans
Copy link

timohermans commented Feb 15, 2024

@GStoynev wow that is a very easy solution 🤔.

One other thing I found out yesterday is that the httpcontext is always available within a HttpClient implementation, wherever you inject it inside the circuit. Which is also what the documentation does, I believe.

@guardrex
Copy link
Collaborator Author

The product unit (PU) is working through their backlog to reach this issue and take a look at the section. I hope we'll have this sorted out no later than the end of next week (2/23).

@guardrex
Copy link
Collaborator Author

guardrex commented Feb 23, 2024

Still trying to get 👁️👁️ on this. I'll try to reach out to Stephen and Jeremy again on Monday.

@guardrex
Copy link
Collaborator Author

guardrex commented Feb 27, 2024

I was just made aware over on a PU issue of the following approach in an Javier sample app ...

https://github.com/javiercn/BlazorWebNonceService/

I'll take a look at that to resolve this issue ... hopefully within a couple of days.

No 🎲🎲 .... That only addresses the nonce situation. Also, that sample doesn't match the approach that Stephen adopted for the BWA on dotnet/blazor-samples#240.

@Kumima
Copy link

Kumima commented Sep 12, 2024

@pmi24
For InteractiveServer, HttpContext can actually be accessible without that approach, such as using IHttpContextAccessor. But I think that HttpContext is something like readonly. The reason why you meet the problem is SignInManager.SignInAsync() needs to set the cookie which needs to write the HttpContext. So, the login page in the default template is rendered as static.

Blazor authentication is really a complicated topic, especially now we have the Auto mode. I've notice there are many issues raised to discuss what's the best approach, I hope the documentation will keep improving this.

@huazhb
Copy link

huazhb commented Nov 30, 2024

@pmi24 For InteractiveServer, HttpContext can actually be accessible without that approach, such as using IHttpContextAccessor. But I think that HttpContext is something like readonly. The reason why you meet the problem is SignInManager.SignInAsync() needs to set the cookie which needs to write the HttpContext. So, the login page in the default template is rendered as static.

Blazor authentication is really a complicated topic, especially now we have the Auto mode. I've notice there are many issues raised to discuss what's the best approach, I hope the documentation will keep improving this.

Finally, I found someone noted InteractiveServer does valid for HttpContext and IHttpContextAccessor. The Microsoft website really confusion which state "IHttpContextAccessor]must be avoided with interactive rendering because there isn't a valid HttpContext available.". I think only the code that run at the client side will NOT valid for HttpContext like InteractiveClient or InteractiveAuto that after first initialization.

@guardrex guardrex moved this from 8.0 to P0/P1 - High Priority in Blazor.Docs Dec 6, 2024
@guardrex guardrex removed the 8.0 .NET 8 label Dec 6, 2024
@guardrex guardrex moved this from P0/P1 - High Priority to 10.0 in Blazor.Docs Dec 11, 2024
@guardrex guardrex added the 10.0 .NET 10 label Dec 11, 2024
@guardrex guardrex moved this from 10.0 to P0/P1 - High Priority in Blazor.Docs Mar 15, 2025
@guardrex guardrex removed the 10.0 .NET 10 label Mar 15, 2025
@guardrex guardrex moved this from P0/P1 - High Priority to In progress in Blazor.Docs Mar 25, 2025
@guardrex guardrex moved this from In progress to P0/P1 - High Priority in Blazor.Docs Mar 25, 2025
@nicholasyin
Copy link

@pmi24 For InteractiveServer, HttpContext can actually be accessible without that approach, such as using IHttpContextAccessor. But I think that HttpContext is something like readonly. The reason why you meet the problem is SignInManager.SignInAsync() needs to set the cookie which needs to write the HttpContext. So, the login page in the default template is rendered as static.
Blazor authentication is really a complicated topic, especially now we have the Auto mode. I've notice there are many issues raised to discuss what's the best approach, I hope the documentation will keep improving this.

Finally, I found someone noted InteractiveServer does valid for HttpContext and IHttpContextAccessor. The Microsoft website really confusion which state "IHttpContextAccessor]must be avoided with interactive rendering because there isn't a valid HttpContext available.". I think only the code that run at the client side will NOT valid for HttpContext like InteractiveClient or InteractiveAuto that after first initialization.

Yes, by means of debugging I see that the same code gets run twice, in SSR. The first time HttpContext has value while in the second time it's null. So all in all, 'should be avoided' is correct.

@guardrex
Copy link
Collaborator Author

The sample app consists of two projects:

Update to "three projects."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: In progress
Development

Successfully merging a pull request may close this issue.