Adding the GenerateRandomPassword() method

The username isn't the only "missing" required field we need to take care of when a user registers themselves with a third-party provider, and we want to create their account to our existing identity data model; we also need to generate a password.

Such a task is not trivial as it might seem, for at least two reasons:

  • We cannot set up a weak password, otherwise other users might be able to break into that user's account
  • We cannot set up a weak password, because the ASP.NET Core Identity will refuse it

More specifically, if we recall correctly, what we did back in Chapter 8, Authentication and Authorization, when we added the Identity service to the Startup.cs file, we need a password with at least a digit, a lowercase character, an uppercase character, and a minimum length of seven. The uppercase/lowercase requirements will even cut out our usual "random GUIDs" way of doing (sample) things, unless we want to add or replace some characters manually.

Long story short, the best thing we can do is to implement a quick-and-simple password generator helper function that will generate them according to our standards. From Solution Explorer, right-click on the /Data/ folder, add a new DataHelper.cs file, and fill it with the following content:

using Microsoft.AspNetCore.Identity;
using System;
using System.Collections.Generic;
using System.Linq;

namespace TestMakerFreeWebApp.Data
{
public static class DataHelper
{
/// <summary>
/// Generates a Random Password
/// respecting the given strength requirements.
/// </summary>
/// <param name="opts">A valid PasswordOptions object
/// containing the password strength requirements.</param>
/// <returns>A random password</returns>
public static string GenerateRandomPassword(PasswordOptions
opts = null)
{
if (opts == null) opts = new PasswordOptions()
{
RequiredLength = 7,
RequiredUniqueChars = 4,
RequireDigit = true,
RequireLowercase = true,
RequireNonAlphanumeric = false,
RequireUppercase = true
};

string[] randomChars = new[] {
"ABCDEFGHJKLMNOPQRSTUVWXYZ", // uppercase
"abcdefghijkmnopqrstuvwxyz", // lowercase
"0123456789", // digits
"!@$?_-" // non-alphanumeric
};
Random rand = new Random(Environment.TickCount);
List<char> chars = new List<char>();

if (opts.RequireUppercase)
chars.Insert(rand.Next(0, chars.Count),
randomChars[0][rand.Next(0,
randomChars[0].Length)]);

if (opts.RequireLowercase)
chars.Insert(rand.Next(0, chars.Count),
randomChars[1][rand.Next(0,
randomChars[1].Length)]);

if (opts.RequireDigit)
chars.Insert(rand.Next(0, chars.Count),
randomChars[2][rand.Next(0,
randomChars[2].Length)]);

if (opts.RequireNonAlphanumeric)
chars.Insert(rand.Next(0, chars.Count),
randomChars[3][rand.Next(0,
randomChars[3].Length)]);

for (int i = chars.Count; i < opts.RequiredLength
|| chars.Distinct().Count() < opts.RequiredUniqueChars;
i++)
{
string rcs = randomChars[rand.Next(0,
randomChars.Length)];
chars.Insert(rand.Next(0, chars.Count),
rcs[rand.Next(0, rcs.Length)]);
}

return new string(chars.ToArray());
}
}
}

That will do for now.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.151.32