ASP.NET MVC- 사용자로 LOGGIN을 위해 외부 웹 사이트를 사용하여 필터 작업을 사용자 정의

StackOverflow https://stackoverflow.com/questions/835672

문제

사용자가 인증되지 않은 경우 사용자를 사인 페이지로 전달하는 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은 실행되지 않습니다. 이것이 바로 내가 원하는 것입니다.

이제 내 CustomAuthorize를 변경하여 외부 웹 사이트에 대해 사용자를 인증하고 내 자신의 서명 조치가 아니라 다음과 같은 일을하고 싶다고 말합니다.

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 필터의 두 번째 버전을 실행 한 후에는 실행이 내가 원하는 것이 아닌 MyAction과 계속됩니다! 이 경우 필터 후 MyAction 실행을 어떻게 중지합니까?

-업데이트- 방금 새로운 문제를 발견했습니다. 내 MVC 응용 프로그램은 iframe에 있으며 리디렉션이 리디렉션 후 메인 프레임으로 전류 프레임을 강제로 강제하기를 원하므로 다음과 같은 작업을 수행하고 싶습니다.

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

javaScript를 리디렉션 ()로 전달하는 방법이 있습니까?

도움이 되었습니까?

해결책

필터 컨텍스트에서 결과를 교체하기 전에 Rediptorouteresult를 사용하는 방법과 유사한 리디렉션을 사용하십시오.

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

다른 팁

내가 이해하는지 보자 - 당신은 iframe이 있고이 iframe 내에서 행동을 실행합니다. 해당 IFRAME 내에있는 부모 페이지로 리디렉션하고 싶습니까?

그렇다면 동작에서 리디렉션 (URL)을 사용하십시오.

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

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

또는

'FilterContext.Result'action 'onactionExecuting'편집

filterContext.Result = new ContentResult() { Content = "<script>top.window.location.href='/user/Login'</script>" };
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top