2

I'm using default ASP.NET Core 1.1 template with Individual User Accounts authentication. The following code on VS2015 is complaining about the password requirements (such as length requirements, upper case, lower case etc.). I know we can set these requirements on the built-in RegisterViewModel by using DataAnnotations. But since I'm creating the users programmatically without using any ViewModel, the DataAnnotations will not work. Question: How can I change the password requirements and be able to run the following code:

List<String> usersList = GetAllUsers();

foreach (string s in usersList)
{
    var user = new ApplicationUser { UserName = s, UserRole = "TestRole" };
    var result = await _userManager.CreateAsync(user, "testpassword");
}
nam
  • 21,967
  • 37
  • 158
  • 332
  • You can only achieve this by either calling the store directly, bypassing usermanager (bad idea) or by directly writing into the users table (worse idea). The password requirements are set in Startup.cs (by AddIdentity or you can explicitly override it, but then its valid for the whole application). Best to create a secure password, even when you create the user in Code – Tseng Mar 06 '17 at 01:35
  • @Tseng It's a very simple app with specific password requirements that doe not meet all the default requirements for the password. **Question**: 1. How can I replace the last line of code above with a code using `userStore`. 2. If I were to use users table directly (using `T-SQL`) how do I hash the password? – nam Mar 06 '17 at 03:38

2 Answers2

2

You can add options in the AddIdentity method...

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>();
}
travis.js
  • 5,193
  • 1
  • 24
  • 21
  • The **AddIdentity** in my **startup.cs** file is showing only `services.AddIdentity() .AddEntityFrameworkStores() .AddDefaultTokenProviders();` – nam Mar 06 '17 at 03:56
  • Yes, the options part is what you will need to add to change the password options – travis.js Mar 06 '17 at 03:58
0

There is an up-to-date tutorial on the Microsoft Docs website.

Introduction to Identity on ASP.NET Core

There is a bit more information in the example code for other settings that you can work with, like the "options.Password."

Community
  • 1
  • 1