c# - Generate Random Password which meets Active Directory Password Policy Complexity Requirements Programmatically -


i trying setpassword forget password functionality.

public string setpassword(string username, string randompassword) {     string result = string.empty;     principalcontext ctx = new principalcontext(contexttype.domain);     userprincipal user = userprincipal.findbyidentity(ctx, username);     aduser aduser = new aduser();     if (user != null)     {         user.setpassword(randompassword);         result = "success";     }     return result; } 

i need generate random password meets following complexity:

  • not contain user's account name or parts of user's full name exceed 2 consecutive characters
  • be @ least 6 characters in length
  • contain characters 3 of following 4 categories:
    • english uppercase characters (a through z)
    • english lowercase characters (a through z)
    • base 10 digits (0 through 9)
    • non-alphabetic characters (for example, !, $, #, %)

complexity requirements enforced when passwords changed or created.

is there inbuilt method serves above requirements? have used below method generate password randomely:

string randompassword = membership.generatepassword(8, 0).replace('<','!').replace('>', '#'); 

it throws error when trying set password. appreciate if there , validation or inbuilt method achieve above requirement.

see if works you. wrote .net identity 2 should point in right direction. can see how i'm using on github

var validator = new passwordvalidator {     requiredlength = 6,     requirenonletterordigit = false,     requiredigit = true,     requirelowercase = true,     requireuppercase = true };  passwords.add(generatepassword(validator));   private static string generatepassword(passwordvalidator passwordvalidator) {     var rnd = new random();      while (true)     {         var password = membership.generatepassword(passwordvalidator.requiredlength, 0);         if ((passwordvalidator.requiredigit && !password.any(char.isdigit)) || (passwordvalidator.requirelowercase && !password.any(char.islower)) || (passwordvalidator.requireuppercase && !password.any(char.isupper)))             continue;          if (!passwordvalidator.requirenonletterordigit) password = regex.replace(password, @"[^a-za-z0-9]", m => rnd.next(0, 10).tostring());         return password;     } } 

Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -