简体   繁体   English

如何通过成员身份创建用户,而无需在创建后登录到其帐户?

[英]How do I create a user through membership without being logged into their account after creation?

I'm having a logged in user create another user (different roles) using Membership.CreateUser. 我有一个登录用户,使用Membership.CreateUser创建另一个用户(不同的角色)。 After creating the user, the logged in user automatically gets the boot and the newly created user is logged in. 创建用户后,已登录的用户将自动获得启动,并且新创建的用户已登录。

The goal would be to have the logged in user create a new user and continue working within the app being logged in under their own credentials. 目标是让登录用户创建一个新用户,并继续使用自己的凭据在正在登录的应用程序内工作。

I'm not sure how to change this. 我不确定如何更改此设置。 Any help would be appreciated. 任何帮助,将不胜感激。

Here is my 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");
    }

Try deleting these two lines. 尝试删除这两行。

Roles.AddUserToRole(model.Email, "Client"); 
FormsAuthentication.SetAuthCookie(model.Email, false /*create persistent cookie*/); 

暂无
暂无

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

相关问题 一旦用户通过Asp.Net中的成员资格和角色创建新帐户,我该如何进行身份验证 - How do i authenticate once the user create a new Account by Membership & Roles in Asp.Net 如何在创建帐户后将用户返回到应用程序 - How to return a user to application after account creation 如何TDD自定义成员资格提供程序和自定义成员资格用户? - How do I TDD a custom membership provider and custom membership user? 如何禁用ASP.NET成员资格提供程序的帐户? - How do I disable an account with the ASP.NET Membership Provider? 简单成员资格:我可以在不引用webmatrix的情况下创建用户吗? - Simple Membership: can I create a user without referencing webmatrix? 如何创建没有密码的新用户帐户 - How to create new user account without password 如何将登录用户更改为其他用户? - How do I change the logged in user to another? 通过C#MVC3中的成员身份创建新用户时,如何使用生成的密码或临时密码? - How do I use a generated or temporary password when creating a new user through membership in C# MVC3? 在ASP.NET中,是否有一种方法可以在不将用户实际提交到成员资格数据库的情况下测试成员资格的创建? - In ASP.NET, is there a way to test membership creation without actually committing the user to the membership database? ASP.NET成员资格:如何将用户设置为已登录 - ASP.NET Membership: how to set the user as logged in
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM