사용자가 인증되지 않은 경우 사용자를 로그인 페이지로 전달하는 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에 내가 리디렉션 리디렉션 후 메인 프레임과 현재 프레임을 강제로 원하는, 그래서 내가 좋아하는 일을하고 있어요 ?
는 팁을 주셔서 감사합니다. 방금 새로운 문제를 발견했습니다. 내 MVC 응용 프로그램은 iFrame에 있고 리디렉션 후에 현재 프레임을 기본 프레임으로 강제로 리디렉션하려는 경우 다음과 같은 작업을 수행합니다. filterContext.HttpContext.Response.Write (""); RedirectResult()에 자바 스크립트를 전달하는 방법이 있습니까? – xraminx
리디렉션을 수행하기 위해 올바른 자바 스크립트가 포함 된 실제보기를 되돌릴 수 있습니다. 시도 할 수도있는 JavaScriptResult도 있습니다. 나는 그것을 사용하지 않았다. – tvanfosson
JavaScriptResult는
블록 내에 반환 된 자바 스크립트를 작성합니다! 이유를 모르겠다. 나는 단순히 나를 위해 포워딩을 수행하고 위에서 설명한 첫 번째 사례에서했던 것처럼 그 행동으로 전달한 모조품을 만들었습니다. 도와 줘서 고마워. :) – xraminx