简体   繁体   English

通过C#MVC3中的成员身份创建新用户时,如何使用生成的密码或临时密码?

[英]How do I use a generated or temporary password when creating a new user through membership in C# MVC3?

I'm creating a client contact using Membership.CreateUser and I'd like to supply a generated password each time a contact is created. 我正在使用Membership.CreateUser创建一个客户联系人,我想在每次创建联系人时提供一个生成的密码。 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. 可能是由于您的默认密码不符合ASP.NET成员资格默认密码复杂性要求。 The default password needs to be 7 characters . 默认密码为7个字符 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. 有关如何配置成员资格的minRequiredPasswordLength设置的更多详细信息,请参见下面的MSDN页面。

Membership.MinRequiredPasswordLength Property Membership.MinRequiredPasswordLength属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM