asp.net-core - 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;
}
解决方案
您需要在ApplicationUser
类中使用自动实现的属性,而不仅仅是使用公共字段。这可能是问题所在。
public class ApplicationUser : IdentityUser
{
public String FirstName { get; set; }
public String LastName { get; set; }
public String Organization { get; set; }
}
推荐阅读
- java - Java 保持 pojo 数据存储独立
- c# - 是否可以从 ASP.NET MVC 中的视图返回多个项目
- arrays - 如何从列表中删除所有重复项并使用 Python 更改对重复项的所有引用
- javascript - 属性在控制台中定义,但在代码中不可访问
- html - 如何做文本溢出:反向省略号?
- php - 发表评论时,Wordpress 会删除我的变量
- reactjs - useHook 在代码定义中返回两个元素,如何仅在功能组件中使用时返回一个?
- python - 将多个输入放入列表
- docker - AWS SageMaker TensorFlow Serving - 端点故障 - CloudWatch 日志参考:“NET_LOG:进入事件循环......”
- python - Django - 无法获取自定义日志记录处理程序类