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

Redirection and reloading is not working with authentication implemented in Razor Class Library

$
0
0

I have implemented authentication in a RCL in Blazor project. When I click on Register link it is displaying Restration page (From RCL). When I click on submit it is creating user data in Aspnetuser table but when calling

RedirectManager.RedirectTo("Account/RegisterConfirmation",    new() { ["email"] = Input.Email, ["returnUrl"] = ReturnUrl });

It is throwing IdentityRedirectManager can only be used during static rendering and not navigating to RegisterConfirmation page exception.

Below is my code. 

register.razor

public async Task RegisterUser(EditContext editContext)     {         var user = CreateUser();         await UserStore.SetUserNameAsync(user, Input.Email, CancellationToken.None);         var emailStore = GetEmailStore();         await emailStore.SetEmailAsync(user, Input.Email, CancellationToken.None);         var result = await UserManager.CreateAsync(user, Input.Password);         if (!result.Succeeded)         {             identityErrors = result.Errors;             return;         }         Logger.LogInformation("User created a new account with password.");         var userId = await UserManager.GetUserIdAsync(user);         var code = await UserManager.GenerateEmailConfirmationTokenAsync(user);         code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));         var callbackUrl = NavigationManager.GetUriWithQueryParameters(             NavigationManager.ToAbsoluteUri("Account/ConfirmEmail").AbsoluteUri,             new Dictionary<string, object?> { ["userId"] = userId, ["code"] = code, ["returnUrl"] = ReturnUrl });         await EmailSender.SendConfirmationLinkAsync(user, Input.Email, HtmlEncoder.Default.Encode(callbackUrl));         if (UserManager.Options.SignIn.RequireConfirmedAccount)         {             RedirectManager.RedirectTo("Account/RegisterConfirmation",                 new() { ["email"] = Input.Email, ["returnUrl"] = ReturnUrl });         }         await SignInManager.SignInAsync(user, isPersistent: false);         RedirectManager.RedirectTo(ReturnUrl);     }

RegisterConfirmation.razor

protected override async Task OnInitializedAsync(){    if (Email is null)    {        RedirectManager.RedirectTo("");    }    var user = await UserManager.FindByEmailAsync(Email);    if (user is null)    {        HttpContextAccessor.HttpContext.Response.StatusCode = StatusCodes.Status404NotFound;        statusMessage = "Error finding user for unspecified email";    }    else if (EmailSender is IdentityNoOpEmailSender)    {        // Once you add a real email sender, you should remove this code that lets you confirm the account        var userId = await UserManager.GetUserIdAsync(user);        var code = await UserManager.GenerateEmailConfirmationTokenAsync(user);        code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));        emailConfirmationLink = NavigationManager.GetUriWithQueryParameters(            NavigationManager.ToAbsoluteUri("Account/ConfirmEmail").AbsoluteUri,            new Dictionary<string, object?> { ["userId"] = userId, ["code"] = code, ["returnUrl"] = ReturnUrl });    }}

Viewing all articles
Browse latest Browse all 4839

Trending Articles