views:

1402

answers:

1

I have a CustomeAuthorize action filter that forwards the user to signin page if user is not authenticated. I apply this filter to actions or controllers.

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

and the filter looks like this:

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);
        }
    }
}

Once I assign a value to filterContext.Result, after execution of filter finishes, the execution is (somehow?!) redirected to the SignIn action and MyAction does not execute. This is exactly what I want.

Now say I want to change my CustomAuthorize to authenticate the user against an external website and not my own SignIn action so I am doing something like this:

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);
        }
    }
}

My problem is that after the execution of the second version of CustomAuthorize filter is finished, execution continues to MyAction which is not what I want! How do I stop the execution of MyAction after filter in this case?

-Update- I just came across a new issue. My MVC application is in an iFrame and I want the Redirect to force the current frame as the main frame after redirection, so I am doing something like:

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

Is there a way to pass a javascript to RedirectResult()?

+2  A: 

Use the RedirectResult similar to how you were using the RedirectToRouteResult before to replace the result in the filter context.

filterContext.Result = new RedirectResult("http://externalSite.com/login?returnUrl=" + filterContext.HttpContext.Request.RawUrl );
tvanfosson
Thank you for the tip. I just came across a new issue. My MVC application is in an iFrame and I want the Redirect to force the current frame as the main frame after redirection, so I am doing something like:filterContext.HttpContext.Response.Write("<script type=\"text/javascript\">\ntop.location.href = \"" + "http://externalSite.com/login?returnUrl=" + filterContext.HttpContext.Request.RawUrl + "\";</script>");Is there a way to pass a javascript to RedirectResult()?
xraminx
You might want to try passing back an actual View that contains the correct Javascript to do the redirect. There is a JavaScriptResult, too, that you might try. I haven't used that.
tvanfosson
the JavaScriptResult would write the returned javascript within <pre> </pre> block! Dont know why. I simply made a dummy action the did just the forwarding for me and forwarded to that action just as I did in the first case demonstrated above. Thanks for your help :)
xraminx