Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

MudBlazor Dialog Closes Automatically After a Few Seconds in MainLayout

$
0
0

I'm using Azure AD Authentication in my Blazor WebAssembly application, and I have configured App.razor like this:

<CascadingAuthenticationState><Router AppAssembly="@typeof(Program).Assembly"><Found Context="routeData"><AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)"><Authorizing><MudPaper Class="pa-6 text-center"><MudProgressCircular Indeterminate="true" Color="Color.Primary" Size="Size.Large" Class="mb-4" /><MudText Typo="Typo.h6">@Localizer["Please wait, we are authorizing you..."]</MudText></MudPaper></Authorizing><NotAuthorized><NotAuthorized/></NotAuthorized></AuthorizeRouteView><FocusOnNavigate RouteData="@routeData" Selector="h1" /></Found></Router>

I have MainLayout as the default layout. Here is its structure:

@inherits LayoutComponentBase<MudRTLProvider RightToLeft="@LayoutService.IsRTL"><MudPopoverProvider /><MudDialogProvider /><MudSnackbarProvider /><MudLayout><AuthorizeView><Authorized>                ...</Authorized><NotAuthorized><MudAlert Severity="Severity.Info" Class="mt-8 mud-width-full" Style="max-width:500px;">                    Authentication is required, click <MudLink Href="authentication/login">sign in</MudLink></MudAlert></NotAuthorized></AuthorizeView></MudLayout></MudRTLProvider>

Once the user is authenticated, I need to check their profile data and, based on that, show a MudBlazor dialog.

So, I implemented the logic in OnAfterRenderAsync:

[CascadingParameter] private Task<AuthenticationState> AuthState { get; set; } = default!;private IDialogReference? _dialogRef;private bool _dialogOpened;protected override async Task OnAfterRenderAsync(bool firstRender){    await base.OnAfterRenderAsync(firstRender);    if (firstRender)    {        // At this point, the user might not be authenticated yet since MainLayout is the default layout        await DoUiThings();        StateHasChanged();    }    var user = (await AuthState).User;    if (user.Identity is { IsAuthenticated: true } && !_dialogOpened) // Open dialog only if the user is authenticated and the dialog hasn't been opened yet    {        await ShowDialogIfAuthenticatedAsync();    }}private async Task ShowDialogIfAuthenticatedAsync(){    _dialogRef = await DialogService.ShowAsync<GenericDialog>("Fake Title", parameters, dialogOptions);    _dialogOpened = true;    var resultDialog = await _dialogRef.Result;    if (resultDialog != null && !resultDialog.Canceled)    {        Console.WriteLine("LOG: Dialog closed with OK");    }    else    {        Console.WriteLine("LOG: Dialog canceled");    }}

The issue:

  • The ShowDialogIfAuthenticatedAsync method is executed correctly.
  • The dialog appears as expected.
  • However, after a few seconds, it closes automatically, and I don't understand why.I need to open the dialog inside MainLayout because, based on the user's selection inside the dialog, they will be redirected to another page.

How can I keep the dialog open inside MainLayout without it being closed automatically?


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>