.NET Core UserManager CreateAsync 使 UserName/Email/PhoneNumber 始终为 null
.NET Core UserManager CreateAsync leaves UserName/Email/PhoneNumber always null
NET Core 应用程序为一些额外字段实现自定义 IdentutyUser
逻辑。
我已经成功地使用 CreateAsync
创建了一个用户,但是字段 UserName
、Email
和 PhoneNumber
总是被忽略。它们的规范化版本(NormalizedUserName
、NormalizedEmail
和 NormalizedPhoneNumber
(最后一个是我创建的,因为任何不可为 null 的值都必须是唯一的))创建成功但始终大写.
控制器代码:
[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
模型覆盖 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; }
...
}
我在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;
});
我需要做什么才能将相应的值与其他值一起插入到数据库中?
添加自定义属性后,您应该在 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 xx
,update-database
让 ef 在 AspNetUsers
table 中创建列。
NET Core 应用程序为一些额外字段实现自定义 IdentutyUser
逻辑。
我已经成功地使用 CreateAsync
创建了一个用户,但是字段 UserName
、Email
和 PhoneNumber
总是被忽略。它们的规范化版本(NormalizedUserName
、NormalizedEmail
和 NormalizedPhoneNumber
(最后一个是我创建的,因为任何不可为 null 的值都必须是唯一的))创建成功但始终大写.
控制器代码:
[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
模型覆盖 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; }
...
}
我在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;
});
我需要做什么才能将相应的值与其他值一起插入到数据库中?
添加自定义属性后,您应该在 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 xx
,update-database
让 ef 在 AspNetUsers
table 中创建列。