コード例 #1
0
        public ActionResult Register(RegisterModel model)
        {
            model.Password = "******";
                // Attempt to register the user
                try
                {
                    WebSecurity.CreateUserAndAccount(model.UserName, model.Password);

                    if (String.IsNullOrEmpty(model.Role))
                    {
                        var roles = Roles.GetRolesForUser(model.UserName);
                        foreach (var role in roles)
                        {
                            Roles.RemoveUserFromRole(model.UserName, role);
                        }
                    }
                    else
                    {
                        // Add Role to User
                        if (!Roles.RoleExists(model.Role))
                            Roles.CreateRole(model.Role);
                        Roles.AddUserToRole(model.UserName, model.Role);
                    }

                    WebSecurity.Login(model.UserName, model.Password);
                    return RedirectToAction("Index", "Home");
                }
                catch (MembershipCreateUserException e)
                {
                    ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
                }

            ViewBag.Role = getRoleSelectList();

            // If we got this far, something failed, redisplay form
            return View(model);
        }
コード例 #2
0
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                try
                {
                    WebSecurity.CreateUserAndAccount(model.UserName, model.Password);

                    // Add Role to User
                    if (!Roles.RoleExists(model.Role))
                        Roles.CreateRole(model.Role);
                    Roles.AddUserToRole(model.UserName, model.Role);

                    WebSecurity.Login(model.UserName, model.Password);
                    return RedirectToAction("Index", "Home");
                }
                catch (MembershipCreateUserException e)
                {
                    ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
                }
            }

            ViewBag.Role = getRoleSelectList();

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