I have a MudBlazor web server app in Visual Studio 2022 with this login flow:
Home.razorwithMainLayoutRedirectToLogin.razorLogin.razorwithLoginLayout
In Login.razor, after login, user.Identity.IsAuthenticated is true, but when I redirect to home.razor, the AuthState is missing and I never get logged in:
if (user.Identity.IsAuthenticated){ NavigationManager.NavigateTo("/",true);}I have been trying for many days to solve this, but I can't, any help would be appreciated!
Home.razor:
@page "/"MainLayout.razor:
@inherits LayoutComponentBase<MudThemeProvider IsDarkMode="false" /><MudPopoverProvider /><MudDialogProvider /><MudSnackbarProvider /><PageTitle>Proyectia</PageTitle><MudLayout><CascadingAuthenticationState><AuthorizeView><Authorized><MudAppBar Color="Color.Primary" Dense="true" Elevation="1"><MudIconButton Icon="@Icons.Material.Rounded.Assignment" Color="Color.Inherit" Edge="Edge.Start" OnClick="@((e) => DrawerToggle())" /><MudText Typo="Typo.h5">Proyectia</MudText><MudSpacer /><MudMenu Dense Variant="Variant.Text" Size="Size.Medium" Color="Color.Inherit" Icon="@Icons.Material.TwoTone.MoreVert"><MudMenu StartIcon="@Icons.Material.TwoTone.Settings" IconColor="Color.Primary" Label="Settings"><MudMenuItem Icon="@Icons.Material.TwoTone.DarkMode" IconColor="Color.Secondary" Label="Dark Theme" /></MudMenu><MudDivider /><MudMenuItem Href="/" ForceLoad Icon="@Icons.Material.TwoTone.Logout" IconColor="Color.Primary" Label="Sign Out" /></MudMenu></MudAppBar><MudDrawer @bind-Open="@_drawerOpen" ClipMode="DrawerClipMode.Always" Elevation="1"><MudDrawerHeader><MudText Typo="Typo.h6">Portafolios</MudText></MudDrawerHeader><NavMenu /></MudDrawer><MudMainContent Class="mt-4"> @Body</MudMainContent></Authorized><NotAuthorized><RedirectToLogin /></NotAuthorized><Authorizing><em>Authorizing...</em></Authorizing></AuthorizeView></CascadingAuthenticationState></MudLayout>@code { bool _drawerOpen = true; void DrawerToggle() { _drawerOpen = !_drawerOpen; }}RedirectToLogin.razor:
@inject NavigationManager NavigationManager@code { [CascadingParameter] private Task<AuthenticationState>? AuthState { get; set; } protected override async Task OnInitializedAsync() { var authState = await AuthState!; if (authState?.User?.Identity == null || !authState.User.Identity.IsAuthenticated) { var returnUrl = NavigationManager.ToBaseRelativePath(NavigationManager.Uri); if (string.IsNullOrEmpty(returnUrl)) NavigationManager.NavigateTo("/login", true); else NavigationManager.NavigateTo("/login?returnUrl=" + returnUrl, true); } }}Login.razor:
@using mbzProyecto.Components.Layout@using System.Security.Claims;@inject NavigationManager NavigationManager@layout LoginLayout@page "/login"<MudGrid Class="mt-16" Justify="Justify.Center" Style="align-items: center;" Spacing="4"><MudItem><MudImage Src="images/Proyectia.png" Height="250" Width="250" Alt="Logo" /></MudItem><MudItem Class="ml-8 mr-n5"><MudImage src="images/gray-vertical-line.png" Height="300" /></MudItem><MudItem><MudContainer><MudGrid Class="ml-1"><MudText Typo="Typo.h4">Login Proyectia</MudText> </MudGrid><MudGrid Class="mt-0"><MudItem><MudTextField T="string" Label="Correo electrónico" Variant="Variant.Outlined" @bind-Value=@Email /></MudItem></MudGrid><MudGrid><MudItem><MudTextField T="string" Label="Password" Variant="Variant.Outlined" @bind-Value=@Password /></MudItem></MudGrid><MudGrid Justify="Justify.Center" Class="mt-2"><MudItem><MudButton Color="Color.Info" Variant="Variant.Filled" OnClick="@Authenticate"> Iniciar Sesion<MudIcon Icon="@Icons.Material.Filled.Login" Class="ml-2" /></MudButton></MudItem></MudGrid></MudContainer></MudItem></MudGrid>@code { public string Email { get; set; } public string Password { get; set; } public string Id { get; set; } = "12345"; [CascadingParameter] public Task<AuthenticationState> AuthTask { get; set; } [Inject] private AuthenticationStateProvider AuthState { get; set; } private System.Security.Claims.ClaimsPrincipal user; protected async override Task OnInitializedAsync() { //var authState = await AuthTask; //this.user = authState.User; } public async Task Authenticate() { //Todo: Validate against actual database. var authState = await ((CustomAuthenticationStateProvider)AuthState).ChangeUser(this.Email, this.Id, "Associate"); this.user = authState.User; if (user.Identity.IsAuthenticated) { NavigationManager.NavigateTo("/",true); } }}LoginLayout.razor:
@inherits LayoutComponentBase<MudThemeProvider IsDarkMode="false" /><PageTitle>Proyectia</PageTitle><MudLayout><MudMainContent Class="mt-4"> @Body </MudMainContent></MudLayout>