visual-studio-2015 - ASP.NET Identity - 如何以编程方式创建用户时更改密码要求

标签 visual-studio-2015 asp.net-core asp.net-identity asp.net-identity-3

我使用默认的 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/

相关文章:

visual-studio - Visual Studio 认为 `Microsoft Web Developer Tools` 已安装

c# - ASP.NET Core 为特定 Controller 禁用 CORS

c# - Asp.net mvc identity SecurityStamp 无处不在注销

c# - 在 Web Forms、MVC、Signalr 中一致访问 IOwinContext

c++ - 为什么我不能使用 auto 声明变量?

c# - Xamarin android 应用程序在 Visual Studio 2015 中的部署错误

xamarin - 错误 : there were deployment errors

c# - ASP.NET 5 Process.Start?

c# - 在 ASP.NET Core3.1 中使用包含 'System.Web.HttpContext' 的遗留项目

asp.net-identity - 在带有身份的 Blazor WebAssembly 中使用授权角色和策略?