所以我需要将一堆用户导入我的基于ASP.NET身份的系统中,并且他的一些密码非常短(只有三个字符)并且超出了我的控制范围。我需要导入这些密码,以便现有用户可以登录并更改密码以满足更严格的要求。
我认为通过在PasswordValidator
类中配置ApplicationUserManager
属性,我可以将导入密码的要求设置为较低的数字,从而允许我导入现有用户。
// Configure validation logic for passwords
appUserManager.PasswordValidator = new PasswordValidator
{
RequiredLength = 3,
RequireNonLetterOrDigit = false,
RequireDigit = false,
RequireLowercase = false,
RequireUppercase = false,
};
但是,当我导入用户并尝试创建新记录时,我在IdentityResult
中收到验证错误。
IdentityResult result = manager.Create(newUser, importedUser.Password);
result.Errors = "Passwords must be at least 6 characters."
是否无法覆盖Identity默认要求?
以下是整个ApplicationUserManager类的参考:
public class ApplicationUserManager : UserManager<ApplicationUser>
{
public ApplicationUserManager(IUserStore<ApplicationUser> store)
: base(store)
{
PasswordValidator = new MinimumLengthValidator(3);
}
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
var appDbContext = context.Get<ApplicationDbContext>();
var appUserManager = new ApplicationUserManager(new UserStore<ApplicationUser>(appDbContext));
// Configure validation logic for usernames
appUserManager.UserValidator = new UserValidator<ApplicationUser>(appUserManager)
{
AllowOnlyAlphanumericUserNames = true,
RequireUniqueEmail = false
};
// Configure validation logic for passwords
appUserManager.PasswordValidator = new PasswordValidator
{
RequiredLength = 3,
RequireNonLetterOrDigit = false,
RequireDigit = false,
RequireLowercase = false,
RequireUppercase = false,
};
appUserManager.EmailService = new Services.EmailService();
var dataProtectionProvider = options.DataProtectionProvider;
if (dataProtectionProvider != null)
{
appUserManager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity"))
{
//Code for email confirmation and reset password life time
TokenLifespan = TimeSpan.FromHours(6)
};
}
return appUserManager;
}
}
答案 0 :(得分:1)
我发现了问题。我从API控制器调用上面列出的ApplicationUserManager
类。但是,我试图将用户导入到API之外的数据库中。所以我需要做的是将密码验证覆盖放在我导入用户的方法中。特别是在我实例化manager
的地方。这是一个例子:
foreach (var user in UserList)
{
var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
manager.PasswordValidator = new MinimumLengthValidator(3);
var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(new ApplicationDbContext()));
var newUser = new ApplicationUser()
{
UserName = user.UserName,
UserId = user.UserId,
Email = user.Email,
FirstName = user.FirstName,
LastName = user.LastName,
};
// Assign the password
IdentityResult result = manager.Create(newUser, user.Password);
// Assign the role
var addedUser = manager.FindByName(user.UserName);
manager.AddToRoles(addedUser.Id, new string[] { "User" });
}
注意第manager.PasswordValidator = new MinimumLengthValidator(3);
行。这是我定义密码覆盖的地方。以前我在ApplicationUserManager
类中放置了密码覆盖,认为它将涵盖manager
创建的所有实例。解决方案是定义使用ApplicationUserManager
类的密码覆盖。很抱歉自己的答案,但我想写出解决方案,以防其他人遇到同样的问题。
答案 1 :(得分:0)
检查你的web.config文件,确保它没有覆盖其他所有内容的最小密码长度要求。