Adding users to roles on registring failed ASP.NET MVC3

谁都会走 提交于 2019-12-08 12:43:27

问题


I like to enable users to choose their role on register in asp.net mvc web application. I have previous add roles via asp.net addministration tool. Here is the code I am using in register methods

public ActionResult Register()  
{
ViewData["roleName"] = new SelectList(Roles.GetAllRoles(), "roleName");
return View();
}


 [HttpPost]
    public ActionResult Register(RegisterModel model)
    {
        if (ModelState.IsValid)
        {
            // Attempt to register the user
            MembershipCreateStatus createStatus;
            Membership.CreateUser(model.UserName, model.Password, model.Email, null, null, true, null, out createStatus);

            if (createStatus == MembershipCreateStatus.Success)
            {
                Roles.AddUserToRole(model.UserName, "roleName");
                FormsAuthentication.SetAuthCookie(model.UserName, false /* createPersistentCookie */);
                return RedirectToAction("Index", "Home");
            }
            else
            {
                ModelState.AddModelError("", ErrorCodeToString(createStatus));
            }
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }

While in Register View I got this code

    <label for="roleName">Select Role:</label>
         @Html.DropDownList("roleName")
         @Html.ValidationMessage("roleName")

Roles are written in the database and users although. However UsersInRoles table is empty and I got this exception

System.Configuration.Provider.ProviderException: The role 'roleName' was not found.

Does anybody had the same problem?


回答1:


You should add roleName parameter to Register controller action for get selected by user roleName, or retrieve roleName from FormCollection, here is example Change your Register post action to:

        [HttpPost]
        public ActionResult Register(RegisterModel model, String roleName)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                MembershipCreateStatus createStatus;
                Membership.CreateUser(model.UserName, model.Password, model.Email, null, null, true, null, out createStatus);

                if (createStatus == MembershipCreateStatus.Success)
                {
                    Roles.AddUserToRole(model.UserName, roleName);
                    FormsAuthentication.SetAuthCookie(model.UserName, false /* createPersistentCookie */);
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    ModelState.AddModelError("", ErrorCodeToString(createStatus));
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }


来源:https://stackoverflow.com/questions/13116969/adding-users-to-roles-on-registring-failed-asp-net-mvc3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!