I have a situation, I hope you can help me. I'm having an ERR_TOO_MANY_REDIRECTS error when from the BaseController of an MVC 4.5 application I try to redirect the user who is not logged in to the Login page. It should be mentioned that this page is also configured as a homepage.
Start page code:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Cuenta", action = "IniciarSesion", id = UrlParameter.Optional }
);
}
}
Code in the base controller that redirects to the login page if it is not authenticated:
public class BaseController : Controller
{
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (!System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
{
filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { controller = "Cuenta", action = "IniciarSesion" }));
return;
}
base.OnActionExecuting(filterContext);
}
}
With this code I am receiving the error mentioned above. If you can help me, I'll be grateful. Greetings.