简体   繁体   中英

.NET Core UserManager CreateAsync leaves UserName/Email/PhoneNumber always null

NET Core application implementing a custom IdentutyUser logic for some extra fields.

I have managed to create a user using CreateAsync but the fields UserName , Email and PhoneNumber are always ignored. The normalized version of them ( NormalizedUserName , NormalizedEmail and NormalizedPhoneNumber (the last one was created by me as any non-nullable value is required to be unique)) are created successful but always capitalized.

The Controller code:

[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;
    }
}

If it helps the problematic fields are being overriden by a ApplicationUser model class:

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; }

    ...
}

I also have this code in 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;
});

What do I have to do to insert the corresponding values into the db along with the rest?

After adding the custom properties , you should use the new ApplicationUser in ConfigureServices:

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

And ApplicationDbContext :

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>

And confirm that you inject the UserManager with new ApplicationUser in controller:

private readonly UserManager<ApplicationUser> _userManager;

At last Add-migration xx , update-database to let ef to create columns in AspNetUsers table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM