2

다른 작업에서 컨트롤러 작업을 호출 할 때 RedirectToAction을 사용해야합니까? 나는 그 (것)들을 돌려 보내는 원하지 않기 때문에 나는 지금 막 그 (것)들을 직접 부른다, 그래서 나는 나의 활동 중 하나에 Authorize 꼬리표를 우회한다 (나가 원하는 무슨을).MVC3 권한 부여 : 다른 작업에서 승인 된 조치를 호출하는 것이 나쁜 형태입니까?

이 형식이 잘못된 경우 알려 주실 수 있습니다. 그렇다면 클라이언트 쿠키를 설정하거나 LogOn() 작업에 직접 설정하는 여러 가지 새로운 작업을 만들어야합니까?

대신 을 비공개로 설정 한 다음 클라이언트 측의 관리자 만 공용 권한 부여 작업을 사용할 수 있습니까? 그런 다음 개인 작업은 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); 
     } 
+2

승인을 무시합니까? 그렇다면 절대적으로하지 마십시오. 내부 및 외부 방법은 차이가 있습니다 (단지 공개/비공개 또는 서비스에 노출/노출되지 않음). –

+0

'LogOn()'액션에서'SwitchClient'를 호출하기 전에 이미'FormsService.SignIn (model.UserName, model.RememberMe);를 호출했기 때문에 우회하지 않는다고 말할 수 있습니다. 이것은 어떤 클라이언트에서나 (관리자 역할이든 아니든간에) 작동합니다. –

답변

1

나는 개인 방법 또는 유틸리티 클래스에 "스위치 클라이언트"논리를 리팩토링 것 :

여기 내 코드입니다. 두 컨트롤러 액션 메서드는 private 메서드를 호출합니다.

이렇게하면 코드와 의도가 덜 혼동 스럽습니다.

+0

pst의 코멘트를 읽은 후 고마워요. –