我有一个 WebAPI 应用程序,我想在我为每个用户存储的信息中添加一封电子邮件。虽然有很多例子,但我不确定这些是现在推荐使用 Identity 2 的方法。特别是我注意到 Identity 2 现在有扩展方法来处理电子邮件。
UserManagerExtensions.FindByEmail 方法
但是如何存储电子邮件以便我可以使用此扩展方法?我是否需要将其存储在这里,如果需要,我可以将其存储为索赔吗?
public class ApplicationUser : IdentityUser {
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager) {
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
// I don't want to add email here
}
但是我不确定如何做到这一点:
- 我是否应该将电子邮件添加到上面写着“//在此处添加自定义用户声明”的地方,如果是这样,我该怎么做?
我应该更改下面的注册方法吗?如果是,我应该怎么做?
[AllowAnonymous] [Route("Register")] public async Task<IHttpActionResult> Register(RegisterBindingModel model) { if (!ModelState.IsValid) { return BadRequest(ModelState); } IdentityUser user = new IdentityUser { UserName = model.UserName }; IdentityResult result = await UserManager.CreateAsync(user, model.Password); IHttpActionResult errorResult = GetErrorResult(result); if (errorResult != null) { return errorResult; } return Ok(); }
我会很感激一些关于如何做到这一点的建议。请注意,我在 Identity2 应用程序中使用基于令牌的方法和声明。如果可能的话,我希望通过声明来做到这一点,而不仅仅是向用户表添加列。