添加角色只向用户添加一个角色

时间:2012-04-15 01:49:21

标签: asp.net asp.net-mvc-3 forms-authentication

我的帐户控制器有以下代码:

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

        //
        // POST: /Account/Register

        [HttpPost]
        [AllowAnonymous]
        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, "gamers");
                    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);

        }

我有两个角色

gamers and consoleadders

我的问题是,我如何设置thsi代码,以便当用户选择一个角色时,它会被保存,因为它目前只保存一个角色,即游戏玩家。我可以让角色可见,但我只能选择"gamers"

这是因为这行代码:

Roles.AddUserToRole(model.UserName, "gamers");

我还需要什么,所以当添加用户时,他们可以选择任一角色,并将角色设置为该帐户

由于

0 个答案:

没有答案