首页 > 解决方案 > ASP.NET Core 标识扩展不保存

问题描述

我正在尝试扩展 ASP.NET 标识。我觉得我有大部分的部分。模型和用户对象都正确填充。但是,当我检查数据库是否有通过 CreateAsync 函数插入的新记录时,新字段都是 NULL。我错过了什么?

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Register(RegisterViewModel model, string returnUrl = null)
    {
        ViewData["ReturnUrl"] = returnUrl;
        if (ModelState.IsValid)
        {
            var user = new ApplicationUser { UserName = model.Email, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName
                , Organization = model.Organization };
            var result = await _userManager.CreateAsync(user, model.Password);
            if (result.Succeeded)
            {
                _logger.LogInformation("User created a new account with password.");

                var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
                var callbackUrl = Url.EmailConfirmationLink(user.Id.ToString(), code, Request.Scheme);
                await _emailSender.SendEmailConfirmationAsync(model.Email, callbackUrl);

                await _signInManager.SignInAsync(user, isPersistent: false);
                _logger.LogInformation("User created a new account with password.");
                return RedirectToLocal(returnUrl);
            }
            AddErrors(result);
        }

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



        services.AddIdentity<ApplicationUser, IdentityRole>(options =>
        {
            // Signin settings
            options.SignIn.RequireConfirmedEmail = false;
            options.SignIn.RequireConfirmedPhoneNumber = false;
        })
        .AddEntityFrameworkStores<ApplicationDbContext>()
        .AddDefaultTokenProviders();


public class ApplicationUser : IdentityUser
{
    public String FirstName;

    public String LastName;

    public String Organization;
}

标签: asp.net-coreasp.net-identity

解决方案


您需要在ApplicationUser类中使用自动实现的属性,而不仅仅是使用公共字段。这可能是问题所在。

public class ApplicationUser : IdentityUser
{
    public String FirstName { get; set; }

    public String LastName { get; set; }

    public String Organization { get; set; }
}

推荐阅读