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");
}
2
  • 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 Commented Mar 6, 2017 at 1: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? Commented Mar 6, 2017 at 3:38

2 Answers 2

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>();
}
Sign up to request clarification or add additional context in comments.

2 Comments

The AddIdentity in my startup.cs file is showing only services.AddIdentity<ApplicationUser, ApplicationRole>() .AddEntityFrameworkStores<ApplicationDbContext>() .AddDefaultTokenProviders();
Yes, the options part is what you will need to add to change the password options
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."

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.