Skip to content

Can I use the new razor Identity UI from .NET8 in Blazor Server Side? #53893

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

Closed
twojnarowski opened this issue Feb 8, 2024 · 4 comments
Closed
Labels
area-blazor Includes: Blazor, Razor Components design-proposal This issue represents a design proposal for a different issue, linked in the description ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. question Status: Resolved

Comments

@twojnarowski
Copy link

Summary

In this article What’s new with identity in .NET 8
it is stated that:

The new version is built with Razor components and works with both server-side and WebAssembly Blazor apps.

However here In SSR, supply HttpContext as cascading value #50253 it is stated that:

In SSR, this will receive the HttpContext. In other rendering modes where there is no HTTP context, no value will be supplied so the property will remain null.

So these two statements conflict with each other, because in the new .NET8 Blazor template for example the Login.razor page needs the HttpContext:

[CascadingParameter] private HttpContext HttpContext { get; set; } = default!;

Other pages also use the RedirectManager.RedirectToCurrentPageWithStatus method which needs HttpContext as a parameter.

I have created a new project using this template:
image
And selected the Interactive render mode "Server"
And now when I start this application, I can see during debugging that the HttpContext from Cascading Parameter is not actually null:
image

In this template, the Program.cs has these lines:

builder.Services.AddRazorComponents()
    .AddInteractiveServerComponents();

Am I correct to assume that means I am using what used to be Blazor Server Side nad now Interactive Server Side Rendering? And if yes, then why in such case the HttpContext is not null?

Motivation and goals

I would like to use the new Blazor Identity UI in the application which I updated from .NET7 to .NET8

In scope

Clarification as to how can I use Identity in Blazor SS.
Do I still need _Host.cshtml to extract authentication information from HttpContext during application load?
Can I use new Identity razor components in Blazor SS-only application?
Can I access HttpContext in a Blazor SS-only application?
Is Blazor Server Side now renamed "interactive SSR" in the documentation?

@twojnarowski twojnarowski added the design-proposal This issue represents a design proposal for a different issue, linked in the description label Feb 8, 2024
@ghost ghost added the area-blazor Includes: Blazor, Razor Components label Feb 8, 2024
@javiercn
Copy link
Member

javiercn commented Feb 9, 2024

@twojnarowski thanks for contacting us.

I think the terminology here is a bit confusing:
Blazor SSR = Static Server Rendering (Equivalent to MVC/Razor pages).
Interactive Server = Component running on the server interactively (Blazor Server + static server prerendering by default).
Interactive Client = Component running on the browser (Blazor Webassembly + static server prerendering by default).

App.razor <~> _Host.cshtml
MainLayout.razor <~> _Layout.cshtml
Routes.razor <~> (old App.razor, interactivity root)

With this in mind, the new identity support only works in Static Server Rendering (SSR) as it requires the HttpContext to be available.

  • When you navigate to one of the identity pages, it first prerenders statically on the server as a result of an HTTP request, which is why HttpContext is available.

  • Forms and other aspects of the Identity UI only work in SSR mode, as they require an HttpContext instance, so they won't work if the component becomes interactive (the HttpContext in the cascading value is not provided there).

  • You still need to collect any information from the request and pass it down to any interactive components (what you did in _Host.cshtml, you do in App.razor)

  • You can only access HttpContext during static rendering.

@javiercn javiercn added question ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. labels Feb 9, 2024
@twojnarowski
Copy link
Author

So, I can use the new .NET8 template to create a Blazor Web App with Interactive Server, and with Individual Accounts Identity, but to make it work I need to copy things that I need from HttpContext in App.razor to memory and then change all of the generated UI to use data from memory, not from HttpContext, and it should work correctly?

Copy link
Contributor

This issue has been resolved and has not had any activity for 1 day. It will be closed for housekeeping purposes.

See our Issue Management Policies for more information.

@twojnarowski
Copy link
Author

@javiercn is it possible to get a clarification on my last question, can I copy user claims from HttpContext to memory in App.razor and just modify the new UI to use user data from memory and not from HTTPContext and everything should work?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-blazor Includes: Blazor, Razor Components design-proposal This issue represents a design proposal for a different issue, linked in the description ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. question Status: Resolved
Projects
None yet
Development

No branches or pull requests

2 participants