I’m creating a client contact using Membership.CreateUser and I’d like to supply a generated password each time a contact is created. The production team will be setting up accounts for new client contacts.
The problem I’m running into is when I pass through the generic password, the method fails. When I supply my own password through the model it works fine.
I’m looking for a work-around or a better practice.
Thanks in advance for you help!
Here is an example of the code:
[HttpPost]
public ActionResult RegisterClientContact(RegisterClientContactViewModel model)
{
if (ModelState.IsValid)
{
//Create Generic password - First initial Last initial 1234
string genericPassword = model.FirstName.Substring(0, 1).ToLower() + model.LastName.Substring(0, 1).ToLower() + "1234";
//Attempt to register the Client Contact
MembershipCreateStatus createStatus;
Membership.CreateUser(model.Email, genericPassword, model.Email, null, null, true, null, out createStatus);
if (createStatus == MembershipCreateStatus.Success)
{
Roles.AddUserToRole(model.Email, "Client");
FormsAuthentication.SetAuthCookie(model.Email, false /*create persistent cookie*/);
Person person = new Person();
person.FirstName = model.FirstName;
person.LastName = model.LastName;
person.Email = model.Email;
person.Phone = model.Phone;
person.Active = true;
db.Persons.Add(person);
db.SaveChanges();
ClientContact clientPerson = new ClientContact();
clientPerson.ClientPersonId = person.Id;
clientPerson.Title = model.Title;
clientPerson.ClientId = model.ClientId;
db.ClientPersons.Add(clientPerson);
db.SaveChanges();
return RedirectToAction("Index", "ClientContact");
}
}
return View("An Error has occured");
}
By the way, it seems I should be able to pass in my own password given this article.
It’s likely due to the fact that your default passwords don’t meet the ASP.NET Membership default password complexity requirements. The default password needs to be 7 characters. The exception should tell you exactly what the problem is. See the following MSDN page for more details on how to configure Membership’s minRequiredPasswordLength setting.
Membership.MinRequiredPasswordLength Property