【发布时间】:2020-05-05 20:01:48
【问题描述】:
NET Core 应用程序为一些额外字段实现了自定义 IdentutyUser 逻辑。
我已经设法使用CreateAsync 创建了一个用户,但UserName、Email 和PhoneNumber 字段总是被忽略。它们的规范化版本(NormalizedUserName、NormalizedEmail 和 NormalizedPhoneNumber(最后一个是我创建的,因为任何不可为空的值都必须是唯一的))创建成功但总是大写。
控制器代码:
[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