我正在使用asp.Identity在我的应用程序中执行用户和角色模块。我创建像这样的用户
var user = new ApplicationUser() { UserName = name, Email = email };
IdentityResult result1 = ApplicationUserManager.AppUserManager.Create(user, password);
它创建了用户,问题是在应用程序管理器中它没有检查重复的电子邮件。我的应用程序管理器看起来像这样
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
var manager = new ApplicationUserManager(new EntityUserStore<ApplicationUser, Account, ApplicationRole, Role>());
AppUserManager = manager;
// Configure validation logic for usernames
manager.UserValidator = new UserValidator<ApplicationUser>(manager)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true
};
// Configure validation logic for passwords
manager.PasswordValidator = new PasswordValidator
{
RequiredLength = 6,
RequireNonLetterOrDigit = false,
RequireDigit = false,
RequireLowercase = false,
RequireUppercase = false,
};
var dataProtectionProvider = options.DataProtectionProvider;
if (dataProtectionProvider != null)
{
manager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity"));
}
return manager;
}
另一个问题是,如果我使用用户名登录它会起作用,但如果我使用emal则返回null。
ApplicationUser user = UserManager.FindByEmail(email); // this returns null
有谁熟悉这个问题?
答案 0 :(得分:1)
您的ApplicationUserManager.AppUserManager.Create
不会验证电子邮件,因为您没有引用ApplicationUserManager上下文,如下所示:
var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
var signInManager = Context.GetOwinContext().Get<ApplicationSignInManager>();
var user = new ApplicationUser() { UserName = name, Email = email };
IdentityResult result = manager.Create(user, password);
if (result.Succeeded)
以上示例var manager
将包含ApplicationUserManager
上下文,电子邮件的验证将由RequireUniqueEmail = true
完成。