我使用 asp.net Identity 2.0 让用户登录我的网站,其中身份验证详细信息存储在 SQL 数据库中。 Asp.net Identity 已经以标准方式实现,可以在许多在线教程中找到。
The ApplicationUser
上课于IdentityModels
已扩展为包含自定义属性:
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager, string authenticationType)
{
CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, authenticationType);
return userIdentity;
}
//My extended property
public string Code { get; set; }
}
当我注册新用户时,我通过Code
中的自定义属性RegisterBindingModel
但我不确定如何将此自定义属性插入到 WebUsers 表中。
我做了如下操作,但实际上并没有将此属性与用户名和密码一起插入到表中。
var user = new ApplicationUser() { UserName = userName, Email = model.Email, Code=model.Code };
以及整个函数:
[AllowAnonymous]
[Route("Register")]
public async Task<IHttpActionResult> Register(RegisterBindingModel model)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var userName = !string.IsNullOrEmpty(model.UserName) ? model.UserName : model.Email;
//I set it here but it doesn't get inserted to the table.
var user = new ApplicationUser() { UserName = userName, Email = model.Email, Code=model.Code };
IdentityResult result = await UserManager.CreateAsync(user, model.Password);
if (!result.Succeeded)
{
return GetErrorResult(result);
}
return Ok();
}
我缺少什么?
我正在寻找类似的问题,但找不到答案。