我正在使用asp.net Identity 2.0進行登錄我的網站,其中身份驗證詳細信息存儲在SQL數據庫中。在許多在線教程中都可以找到Asp.net Identity以標準方式實施。如何使用自定義屬性擴展IdentityUser
的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();
}
我缺少什麼? 我在尋找類似的問題,但無法找到答案。
我認爲你的代碼是好的,你確定代碼屬性被填充到'model'參數中嗎? – jumuro
是的,我確定,我檢查過它。但是,當我檢查數據庫中的表時,新用戶出現,但代碼字段始終爲'null'。 – user3378165
如何定義UserManager?看看https://github.com/tjoudeh/AspNetIdentity.WebApi –