0

我有一个登录用户使用 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");
    }
4

1 回答 1

0

尝试删除这两行。

Roles.AddUserToRole(model.Email, "Client"); 
FormsAuthentication.SetAuthCookie(model.Email, false /*create persistent cookie*/); 
于 2012-06-24T16:15:06.180 回答