7

사용자가 인증되지 않은 경우 사용자를 로그인 페이지로 전달하는 CustomeAuthorize 작업 필터가 있습니다. 액션이나 컨트롤러에이 필터를 적용합니다.ASP.NET MVC - 사용자 로그인을 위해 외부 웹 사이트를 사용하는 CustomeAuthorize 필터 동작

[CustumeAuthorize] 
public ActionResult MyAction() 
{ 
    //do something here 
    return View(); 
} 

필터는 다음과 같습니다

public class CustomAuthorizeAttribute : ActionFilterAttribute 
{ 
    public override void OnActionExecuting(ActionExecutingContext filterContext) 
    { 

     if (!currentUserIsAuthenticated) 
     { 

      filterContext.Result = 
       new RedirectToRouteResult(
        new RouteValueDictionary{{ "controller", "Account" }, 
               { "action", "SignIn" }, 
               { "returnUrl", filterContext.HttpContext.Request.RawUrl } 
               }); 
     } 
     else 
     { 
      base.OnActionExecuting(filterContext); 
     } 
    } 
} 

내가 필터 마감 실행 후, filterContext.Result에 값을 할당하면 실행이 로그인의 조치로 리디렉션 (?! 어떻게 든)입니다 MyAction은 실행되지 않습니다. 이것은 내가 원하는 것입니다. 그 실행 후

public class CustomAuthorizeAttribute : ActionFilterAttribute 
{ 
    public override void OnActionExecuting(ActionExecutingContext filterContext) 
    { 

     if (!currentUserIsAuthenticated) 
     { 
      filterContext.HttpContext.Response.Redirect("http://externalSite.com/login?returnUrl=" + filterContext.HttpContext.Request.RawUrl); 
     } 
     else 
     { 
      base.OnActionExecuting(filterContext); 
     } 
    } 
} 

내 문제가 :

은 지금은 내가 이런 일을하고 내 자신의 로그인의 조치 외부 웹 사이트에 대해이 아닌 사용자를 인증하는 내 CustomAuthorize을 변경하고 싶은 말은 CustomAuthorize 필터의 두 번째 버전이 완료되면 MyAction이 계속 실행됩니다. 이 경우 필터 후 MyAction의 실행을 어떻게 중지합니까?

-Update- 방금 ​​새로운 문제를 발견했습니다.) (RedirectResult에 자바 스크립트를 전달하는 방법이 있나요

string url = "http://externalSite.com/login?returnUrl=" + filterContext.HttpContext.Request.RawUrl; 
filterContext.HttpContext.Response.Write("<script type=\"text/javascript\">\ntop.location.href = \"" + url + "\";</script>"); 

: 내 MVC 응용 프로그램은 iframe에 내가 리디렉션 리디렉션 후 메인 프레임과 현재 프레임을 강제로 원하는, 그래서 내가 좋아하는 일을하고 있어요 ?

답변

11

필터 컨텍스트에서 결과를 바꾸려면 이전에 RedirectToRouteResult를 사용했던 것과 비슷한 RedirectResult를 사용하십시오.

filterContext.Result = new RedirectResult("http://externalSite.com/login?returnUrl=" + filterContext.HttpContext.Request.RawUrl); 
+1

는 팁을 주셔서 감사합니다. 방금 새로운 문제를 발견했습니다. 내 MVC 응용 프로그램은 iFrame에 있고 리디렉션 후에 현재 프레임을 기본 프레임으로 강제로 리디렉션하려는 경우 다음과 같은 작업을 수행합니다. filterContext.HttpContext.Response.Write (""); RedirectResult()에 자바 스크립트를 전달하는 방법이 있습니까? – xraminx

+0

리디렉션을 수행하기 위해 올바른 자바 스크립트가 포함 된 실제보기를 되돌릴 수 있습니다. 시도 할 수도있는 JavaScriptResult도 있습니다. 나는 그것을 사용하지 않았다. – tvanfosson

+0

JavaScriptResult는

 블록 내에 반환 된 자바 스크립트를 작성합니다! 이유를 모르겠다. 나는 단순히 나를 위해 포워딩을 수행하고 위에서 설명한 첫 번째 사례에서했던 것처럼 그 행동으로 전달한 모조품을 만들었습니다. 도와 줘서 고마워. :) –
                        
                            
    xraminx
                                
                            
                        
                    

1

iFrame이 있고이 iFrame 내에서 작업을 실행하고 있음을 확인하십시오. 해당 iFrame이 아닌 상위 페이지로 리디렉션 하시겠습니까?

그렇다면 리디렉션 (URL) 만 사용하십시오.

+0

iFrame에서 리디렉션 (url)을하면 iFrame에 URL이 저장됩니다. – Dronz

0

로그인 페이지에서 jQuery로 판단을 추가 할 수 있습니다.

$(function() { 
     if (window != top) { 
      top.location.href = location.href; 
     } 
    }); 

또는

편집 작업에 'filterContext.Result' 'OnActionExecuting'

filterContext.Result = new ContentResult() { Content = "<script>top.window.location.href='/user/Login'</script>" };