Asp.. NET身份问题
本文关键字:问题 身份 NET Asp | 更新日期: 2023-09-27 17:51:24
我使用asp。身份来做我的应用程序中的用户和角色模块。我创建这样的用户
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;
}
另一个问题是,如果我使用用户名登录它的工作,但如果我使用电子邮件它返回null。
ApplicationUser user = UserManager.FindByEmail(email); // this returns null
有人熟悉这个问题吗?
您的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
完成。