我使用默认的 ASP.NET Core 1.1 模板和个人用户帐户
身份验证。 VS2015
上的以下代码提示密码要求(例如长度要求、大写、小写等)。我知道我们可以使用 DataAnnotations
在内置的 RegisterViewModel
上设置这些要求。但由于我以编程方式创建用户而不使用任何 ViewModel,因此 DataAnnotations
将不起作用。 问题:如何更改密码要求并能够运行以下代码:
List<String> usersList = GetAllUsers();
foreach (string s in usersList)
{
var user = new ApplicationUser { UserName = s, UserRole = "TestRole" };
var result = await _userManager.CreateAsync(user, "testpassword");
}
最佳答案
您可以在 AddIdentity 方法中添加选项...
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, IdentityRole>(options =>
{
options.Password.RequireDigit = false;
options.Password.RequireLowercase = false;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireUppercase = false;
options.Password.RequiredLength = 1;
options.User.AllowedUserNameCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@.";
})
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddMvc();
// Add application services.
services.AddTransient<IEmailSender, AuthMessageSender>();
services.AddTransient<ISmsSender, AuthMessageSender>();
}
关于visual-studio-2015 - ASP.NET Identity - 如何以编程方式创建用户时更改密码要求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42616149/