我正在使用 Membership.CreateUser 创建一个客户联系人,并且我想在每次创建联系人时提供一个生成的密码。制作团队将为新的客户联系人设置帐户。
我遇到的问题是当我通过通用密码时,该方法失败。当我通过模型提供自己的密码时,它可以正常工作。
我正在寻找解决方法或更好的做法。
提前感谢您的帮助!
以下是代码示例:
[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");
}
顺便说一句,鉴于这篇文章,我似乎应该能够传递我自己的密码。