Blazor bypasses running client Program.cs and client side DI fails #55648
Labels
area-blazor
Includes: Blazor, Razor Components
✔️ Resolution: Answered
Resolved because the question asked by the original author has been answered.
Status: Resolved
The attached app is a default Blazor template for an app with authentication.
I have made the following changes:
Server Project
App.razor:
Client project
Added a class called
MyObject
.Registered a
MyObject
singleton inProgram.cs
.Added a control called
Shared/LoginStatusControl.razor
.Inject
MyObject
inHome.razor
andLoginStatusControl
.Add
LoginStatusControl
toMainLayout
.Add
@attribute [Authorize]
toHome.razor
.Problem:
In the configuration described it appears that Blazor bypasses running Program.cs. This causes
MyObject
to not be registered as a singleton and ultimately results in this error:Removing
@attribute [Authorize]
fromHome.razor
appears to mitigate this issue however in my production app, all pages including "/" need to direct the user to login if they have not done so.Also, I need to use
new InteractiveWebAssemblyRenderMode(prerender: false)
because I have some controls that render modals and they do not render correctly if I don't use that render mode.Question:
How can I protect all pages with
@attribute [Authorize]
and still have my client side DI work?BlazorAuthExample2.zip
The text was updated successfully, but these errors were encountered: