2

RedirectToAction別のアクションからコントローラー アクションを呼び出すときに使用する必要がありますか? 私は現在、それらを返してほしくないので、それらを直接呼び出すだけです。したがって、Authorizeタグをアクションの1つにバイパスします(これは私が望むことを行います)。

これが悪い形式かどうか教えてください。もしそうなら、複数の新しいアクションを作成してクライアント 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);
        }
4

1 に答える 1

1

「クライアントの切り替え」ロジックをプライベート メソッドまたはユーティリティ クラスにリファクタリングします。どちらのコントローラ アクション メソッドもプライベート メソッドを呼び出します。

このようにして、コードと意図が混乱しにくくなります。

于 2011-08-17T17:10:52.140 に答える