重定向和重新加载不适用于 Razor 类库中实现的身份验证

问题描述 投票:0回答:1

我已经在 Blazor 项目的 RCL 中实现了身份验证。当我单击“注册”链接时,它显示“恢复”页面(来自 RCL)。当我单击“提交”时,它正在 Aspnetuser 表中创建用户数据,但在调用时

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

它抛出IdentityRedirectManager只能在静态渲染期间使用并且不能导航到RegisterConfirmation页面异常。

Below is my code. 

注册.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);
     }

注册确认.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 });
    }
}
.net razor blazor
1个回答
0
投票

您可以只使用 NavigationManager.NavigateTo("Account/RegisterConfirmation") 而不是 RedirectManager.RedirectTo( "账户/注册确认", new() { ["email"] = Input.Email, ["returnUrl"] = ReturnUrl });

它对我有用

© www.soinside.com 2019 - 2024. All rights reserved.