asp.net核心如何在注册身份中添加input.property

asp.net core how add input.property in register identity

你好请帮助我:D

在我正在关注的关于在身份寄存器中添加更多输入的教程中,他这样做了 image1

看第95行,他一开始是new Identity然后改成了他的模型new ApplicationUser

我的问题是我的代码与这段代码不一样请看这个

image2

在第 129-133 行你可以看到它与图像一不同 是新的吗?

像 image1 那样做我应该怎么做?

如果有帮助,这是身份页面注册页面中的 OnPost 方法

public async Task<IActionResult> OnPostAsync(string returnUrl = null)
    {
        returnUrl ??= Url.Content("~/");
        ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
        if (ModelState.IsValid)
        {
            var user = CreateUser();

            await _userStore.SetUserNameAsync(user, Input.Email, CancellationToken.None);
            await _emailStore.SetEmailAsync(user, Input.Email, CancellationToken.None);

            var result = await _userManager.CreateAsync(user, Input.Password);

            if (result.Succeeded)
            {
                _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 = Url.Page(
                    "/Account/ConfirmEmail",
                    pageHandler: null,
                    values: new { area = "Identity", userId = userId, code = code, returnUrl = returnUrl },
                    protocol: Request.Scheme);

                await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
                    $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");

                if (_userManager.Options.SignIn.RequireConfirmedAccount)
                {
                    return RedirectToPage("RegisterConfirmation", new { email = Input.Email, returnUrl = returnUrl });
                }
                else
                {
                    await _signInManager.SignInAsync(user, isPersistent: false);
                    return LocalRedirect(returnUrl);
                }
            }
            foreach (var error in result.Errors)
            {
                ModelState.AddModelError(string.Empty, error.Description);
            }
        }

        // If we got this far, something failed, redisplay form
        return Page();
    }

ApplicationUser 只是扩展了 IdentityUser。

请参考:

https://adrientorris.github.io/aspnet-core/identity/extend-user-model.html#:~:text=ASP.NET%20Core%20Identity%20has%20a%20default%20implementation%20that,identity%20user%2C%20and%20add%20the%20properties%20you%20need.