5

I need to redirect to an external url (let's say "www.google.com") from OnActionExecuting method. Right now I'm using something like this:

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
    if (!HttpContext.Current.User.Identity.IsAuthenticated)
    {
        var redirectUrl = "www.google.com";

        try
        {
            var isAjaxRequest = filterContext.HttpContext.Request.IsAjaxRequest();

            if (isAjaxRequest)
            {
                filterContext.HttpContext.Response.StatusCode = SessionController.CustomHttpRedirect;
                filterContext.HttpContext.Response.StatusDescription = redirectUrl;

                filterContext.Result = new JsonResult
                {
                    Data = new { Redirect = redirectUrl },
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet
                };
            }
            else
            {
                filterContext.Result = new RedirectResult(redirectUrl, true);

            }

                return;
            }
            else
            {
                throw new LoggedOutException();
            }
        }
        catch
        {
            throw new LoggedOutException();
        }
    }
}

The problem is that it's not redirecting me to "www.google.com" but it's redirecting to "http://localhost:1234/www.google.com" (I try it locally). There is any way to solve this ? Thanks

cozmin-calin
  • 421
  • 2
  • 6
  • 19

2 Answers2

5

The problem was verry easy to solve:

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
    if (!HttpContext.Current.User.Identity.IsAuthenticated)
    {
        var redirectUrl = "http://www.google.com";

        try
        {
            var isAjaxRequest = filterContext.HttpContext.Request.IsAjaxRequest();

            if (isAjaxRequest)
            {
                filterContext.HttpContext.Response.StatusCode = SessionController.CustomHttpRedirect;
                filterContext.HttpContext.Response.StatusDescription = redirectUrl;

                filterContext.Result = new JsonResult
                {
                    Data = new { Redirect = redirectUrl },
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet
                };
            }
            else
            {
                filterContext.Result = new RedirectResult(redirectUrl, true);

            }

                return;
            }
            else
            {
                throw new LoggedOutException();
            }
        }
        catch
        {
            throw new LoggedOutException();
        }
    }
}

All I had to do was that when I assigned the value to "redirectUrl", I had tu put http before wwww. This mus be put if you use a SSL conenction and you're trying to redirect from mvc to another domain.

cozmin-calin
  • 421
  • 2
  • 6
  • 19
1

Instead of using:

filterContext.Result = new RedirectResult("www.google.com", true);

Try the following:

filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { controller = "Home", action = "External" , ReturnURL = "www.google.com"}));

and in your (Home) controller create an action called (External) and from there redirect to your external url:

 public class HomeController : Controller
        {
[AllowAnonymous]
         public ActionResult External(string ReturnURL){
           return Redirect(ReturnURL);
        }
    }

You can't directly perform a server side redirect from an ajax response. You could, however, return a JsonResult with the new url and perform the redirect with javascript. see this answer

Community
  • 1
  • 1
Ala
  • 1,505
  • 1
  • 20
  • 36
  • I cannot use filterContext.Result = Redirect("www.google.com"); because I'm not in a controller, I'm in class which inherits "ActionFilterAttribute" – cozmin-calin Aug 26 '15 at 08:41
  • it's not working even with this because when I override the OnActionExecuting() method, I test in the if statement to see if the user is authenticated. With this modifications that you showed to me it's entering in an infinite loop in that if statement (if (!HttpContext.Current.User.Identity.IsAuthenticated) – cozmin-calin Aug 26 '15 at 10:29
  • How did you apply the [Authorize] attribute? – Ala Aug 26 '15 at 10:51
  • I added this filter in the "RegisterGlobalFilters" method from "Global.asax.cs" – cozmin-calin Aug 26 '15 at 11:05
  • I have edited my answer. Add [AllowAnonymous] attribute to the (External) action on the Controller – Ala Aug 26 '15 at 11:34
  • I succeded to exlude this attribute from my action restul using this post ( http://blogs.microsoft.co.il/oric/2011/10/28/exclude-a-filter/ ) but now if I use return Redirect("www.google.com") will redirect me to "https://127.0.0.1/www.google.com". The problem still persist – cozmin-calin Aug 26 '15 at 11:59
  • Now the problem is that if I try to redirect to "www.google.com" it's not working but if I try to redirect to "https://www.google.com/" is working perfectly. – cozmin-calin Aug 26 '15 at 12:33