Skip to content
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

Update section on passing tokens in Blazor Web Apps #31691

Open
guardrex opened this issue Feb 5, 2024 · 13 comments
Open

Update section on passing tokens in Blazor Web Apps #31691

guardrex opened this issue Feb 5, 2024 · 13 comments
Assignees
Labels

Comments

@guardrex
Copy link
Collaborator

guardrex commented Feb 5, 2024

Description

Per our offline discussion, either (or both) @halter73 and @JeremyLikness are to review the Pass tokens to a server-side Blazor app section to either establish the content directly (a PR) or provide me enough detail (e.g., Stephen's remarks fleshed out further) in an issue comment to set up the section for BWAs.

Stephen, Jeremy ... We can remove one of you if only one review of the section is reasonable. I assigned both to merely to help keep this on the radar until we get it checked out.

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

@guardrex guardrex added Source - Docs.ms Docs Customer feedback via GitHub Issue ⌚ Not Triaged labels Feb 5, 2024
@github-actions github-actions bot added the Blazor label Feb 5, 2024
@dotnet dotnet deleted a comment from github-actions bot Feb 5, 2024
@guardrex guardrex added Pri1 High priority, do before Pri2 and Pri3 doc-enhancement and removed Source - Docs.ms Docs Customer feedback via GitHub Issue labels Feb 5, 2024
@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.

@guardrex guardrex added the 8.0 .NET 8 label Feb 27, 2024
Choonster added a commit to Choonster/catalogue-scanner that referenced this issue Mar 9, 2024
@guardrex guardrex changed the title PU review of *Pass tokens to a server-side Blazor app* section Update section on passing tokens in Blazor Web Apps Mar 12, 2024
@guardrex
Copy link
Collaborator Author

UPDATE: I'm emailing again for notes/code/caveats ... and gotchas 😈 ... to address this issue. For now, I've just made the article section refer to the PU issue for further information.

@wildermedeiros
Copy link

wildermedeiros commented Mar 21, 2024

Correct me if I'm wrong, I was searching for some way to access the AccessToken after a successfully authentication to use with role claims, and I knocked my head towards the HttpContext with IHttpContextAccessor in the IClaimsTransformation, but didn't work and I believe that was because of the services life cycle. Going forward, exploring the API, I found another way to access the token that I didn't find clearly in the docs:

.AddOpenIdConnect("Oidc", options =>
{
   // OIDC configs
   
   options.Events.OnTokenValidated = context =>
   {
       var accessToken = context.TokenEndpointResponse!.AccessToken;
       // ...
       return Task.CompletedTask;
   };
});

In case of having dynamic claims or more authentication schemes, the access token can be stored and accessed later on in the IClaimsTransformation implementation, to provide information for configuring the claims. In any case, it is working and operating for my goal. The project is configured to global interactive server mode. The approach in the code above make sense or have caveats?

@bpsc-wkubis
Copy link

I hope this will be resolved sooner rather than later

@shurfa
Copy link

shurfa commented Apr 21, 2024

seems that it takes too long to solve, my project stopped for this problem to overcome, please give it HP.

@guardrex
Copy link
Collaborator Author

UPDATE (4/21): I've noted this weekly to them because I only understand bits and pieces from the discussion that took place on the product unit's issue. I can't resolve it on my own without their help. There's nothing else that I can do but wait for their response. I think that the workload has been very high for them, and that's why it's taking so long to get an answer. I'll continue to remark on this each week on Fridays. Hopefully, it won't take much longer.

@timohermans
Copy link

seems that it takes too long to solve, my project stopped for this problem to overcome, please give it HP.

there have been several suggestions in the issues linked. One of them being using a CircuitHandler to store the token in

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

No branches or pull requests

9 participants