MVC3授权:从另一个动作调用授权操作是不是很糟糕?

时间:2011-08-17 16:59:48

标签: asp.net-mvc-3 forms-authentication redirecttoaction authorize-attribute

从其他操作调用控制器操作时是否需要使用RedirectToAction?我目前只是直接调用它们,因为我不希望它们返回,因此我将Authorize标记绕过我的一个动作(这就是我想要的)。

如果这是不好的形式,请告诉我,如果是,我应该创建多个新动作来设置客户端cookie还是直接在LogOn()动作中设置它们?

我可以将SwitchClient设为私有,然后将公共授权操作仅供客户端的管理员使用吗?然后,将通过LogOn操作调用私有操作,但除非用户被身份验证为管理员,否则无法访问。

这是我的代码:

        [HttpGet]
        [CustomAuthorizeAccess(Roles = "Administrator", RedirectResultUrl = "Unauthorized")]
        public ActionResult SwitchClient(string client)
        {
            if (Request.Cookies["Client"] == null)
            {
                HttpCookie clientCookie = new HttpCookie("Client", client);
                Response.Cookies.Add(clientCookie);
            }
            else
            {
                Response.Cookies["Client"].Value = client;
            }
                return new RedirectResult(Request.UrlReferrer.AbsolutePath);
        }

        [HttpPost]
        public ActionResult LogOn(LogOnModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                if (MembershipService.ValidateUser(model.UserName, model.Password))
                {
                    FormsService.SignIn(model.UserName, model.RememberMe);
                    if (Url.IsLocalUrl(returnUrl))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        //Add user's role to cookies (assumes each user only has one role)
                        string role = Roles.GetRolesForUser(model.UserName).First();
                        HttpCookie roleCookie = new HttpCookie("Role", role);
                        if (role == "client1")
                        {
                            SwitchClient("client1");
                        }
                        else if (role == "client2")
                        {
                          SwitchClient("client2");
                        }
                        else if (role == "Administrator" || role == "client3")
                        {
                           SwitchClient("client3");
                        }
                        //Make role cookie persistent for 7 days
                        //if user selected "Remember Me"
                        if (model.RememberMe)
                        {
                            roleCookie.Expires = DateTime.Today.AddDays(7);
                        }
                        if (Response.Cookies["Role"] != null)
                        {
                            Response.Cookies["Role"].Value = null;
                            Response.Cookies.Remove("Role");
                        }
                        Response.Cookies.Add(roleCookie);
                        return RedirectToAction("Index", "Home");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "The user name or password provided is incorrect.");
                }
            }
            // If we got this far, something failed, redisplay form
            return View(model);
        }

1 个答案:

答案 0 :(得分:1)

我会将“切换客户端”逻辑重构为私有方法或实用程序类。两种控制器操作方法都会调用私有方法。

这样代码和你的意图就不那么容易混淆了。