【问题标题】:.NET Core UserManager CreateAsync leaves UserName/Email/PhoneNumber always null.NET Core UserManager CreateAsync 使 UserName/Email/PhoneNumber 始终为空
【发布时间】:2020-05-05 20:01:48
【问题描述】:

NET Core 应用程序为一些额外字段实现了自定义 IdentutyUser 逻辑。

我已经设法使用CreateAsync 创建了一个用户,但UserNameEmailPhoneNumber 字段总是被忽略。它们的规范化版本(NormalizedUserNameNormalizedEmailNormalizedPhoneNumber(最后一个是我创建的,因为任何不可为空的值都必须是唯一的))创建成功但总是大写。

控制器代码:

[HttpPost]
[AllowAnonymous]
[Route("Registration")]
public async Task<IActionResult> UserRegistration(ApplicationUserModel model)
{
    var applicationUser = new ApplicationUser()
    {
        UserName = model.UserName,
        Email = model.Email,
        PhoneNumber = model.PhoneNumber,
        FirstName = model.FirstName,
        // Other fields that are being inserted normally...
    };

    IdentityResult result = await _userManager.CreateAsync(applicationUser, model.Password);

    try
    {
        return Ok(result);
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

如果它有助于有问题的字段被 ApplicationUser 模型类覆盖:

public class ApplicationUser : IdentityUser
{
    [Column("UserName", TypeName = "nvarchar(24)")]
    [Required(ErrorMessage = "UN_REQUIRED")]
    [StringLength(24, ErrorMessage = "UN_LENGTH", MinimumLength = 3)]
    [RegularExpression(@"[A-Za-z]+(?:[A-Za-z0-9-_])+(?:[_-]\w+)?$", ErrorMessage = "UN_INVALID")]
    public override string UserName { get; set; }

    [Column("Email", TypeName = "nvarchar(64)")]
    [Required(ErrorMessage = "EM_REQUIRED")]
    [StringLength(64, ErrorMessage = "EM_LENGTH", MinimumLength = 13)]
    [EmailAddress(ErrorMessage = "EM_INVALID")]
    public override string Email { get; set; }

    [Column("PhoneNumber", TypeName = "nvarchar(15)")]
    [Phone(ErrorMessage = "PN_INVALID")]
    public override string PhoneNumber { get; set; }

    ...
}

我在Startup.cs也有这个代码:

services.Configure<IdentityOptions>(options => {
    options.User.AllowedUserNameCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_";
    options.User.RequireUniqueEmail = true;
    options.Password.RequireDigit = true;
    options.Password.RequireLowercase = true;
    options.Password.RequireUppercase = true;
    options.Password.RequiredLength = 8;
    options.Password.RequireNonAlphanumeric = false;
});

如何将相应的值与其他值一起插入到数据库中?

【问题讨论】:

    标签: c# sql asp.net-core .net-core asp.net-identity


    【解决方案1】:

    添加自定义属性后,您应该在 ConfigureServices 中使用新的ApplicationUser

    services.AddDefaultIdentity<ApplicationUser>()
            .AddDefaultUI(UIFramework.Bootstrap4)
            .AddEntityFrameworkStores<ApplicationDbContext>();
    

    还有ApplicationDbContext

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    

    并确认您在控制器中使用新的 ApplicationUser 注入 UserManager:

    private readonly UserManager<ApplicationUser> _userManager;
    

    最后Add-migration xxupdate-database 让 ef 在AspNetUsers 表中创建列。

    【讨论】:

    • 这正是我需要的我不知道如何感谢你,但我真的很想我终于可以继续前进了!我所要做的就是用您的解决方案替换AddEntityFrameworks&lt;AuthenticationContext&gt;。非常感谢!
    • 我遇到了同样的问题,我将我的代码与这个代码进行了比较,结果是一样的,但是我的数据库中的 NormalizedEmail 仍然为 null,而其他列(如 UserName)被规范化了。有任何想法吗?提前谢谢你
    猜你喜欢
    • 2020-11-29
    • 2020-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-28
    • 1970-01-01
    相关资源
    最近更新 更多