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

Auto Login in NET 8 Blazor project with .NET Identity authentication

$
0
0

I am working on a .NET 8 Blazor project with .NET Identity authentication. My goal is to automatically log in the user and redirect them within the OnInitializedAsync method based on an OTP provided via a query parameter.

Here is the code snippet:

[SupplyParameterFromQuery]private string Otp { get; set; } = default!;[Inject]SignInManager<ApplicationUser> SignInManager { get; set; } = default!;protected override async Task OnInitializedAsync(){    try    {        if (HttpMethods.IsGet(HttpContext.Request.Method))        {            await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme);        }        if (!string.IsNullOrEmpty(Otp))        {            // Validate and get the user and claims            await SignInManager.SignInWithClaimsAsync(user, isPersistent: false, claims);            NavigationManager.NavigateTo("/test");        }        else        {            NavigationManager.NavigateTo("/error");        }        await base.OnInitializedAsync();    }    catch (Exception)    {        NavigationManager.NavigateTo("/error");    }}

However, when attempting to redirect the user, I am encountering the following exception:

Microsoft.AspNetCore.Components.NavigationException: 'Exception of type 'Microsoft.AspNetCore.Components.NavigationException' was thrown.'

How can I handle this exception or modify my approach to achieve the desired functionality without encountering errors?


Viewing all articles
Browse latest Browse all 4839

Trending Articles



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