私は、認証の詳細がSQLデータベースに格納されている私のWebサイトにユーザーがログインするためにasp.net Identity 2.0を使用しています。 Asp.netアイデンティティは、多くのオンラインチュートリアルで見られるように標準的な方法で実装されています。私は新しいユーザーを登録すると、私はRegisterBindingModel
にCode
カスタムプロパティを渡すが、私はこれを挿入するかどうかはわかりませんカスタムプロパティでIdentityUserを拡張する方法
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; }
}
:
IdentityModels
でApplicationUser
クラスには、カスタムプロパティを含むように拡張されましたカスタムプロパティを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();
}
私は何をしないのですか? 私は同様の質問をしていましたが、これに対する回答が見つかりませんでした。
私はあなたのコードはOKだと思いますが、 'Code'プロパティが' model'パラメータで満たされていると思いますか? – jumuro
はい、私はそれを確認したと確信しています。しかし、データベースのテーブルをチェックすると、新しいユーザーが表示されますが、コードフィールドは常に「null」です。 – user3378165
UserManagerはどのように定義されていますか? https://github.com/tjoudeh/AspNetIdentity.WebApi –