'Authorization Attribute call on Index method is causing infinity loop
I am trying to use authorization attribute on index method as shown below but it causes an infinity loop of index method call.
[Authorization]
[HttpGet]
public ActionResult Index()
{
//here is controller code..
}
Code for Authorization Attribute is as follow.
public class AuthorizationAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
//try
//{
if (HttpContext.Current.Session["username"] != null && HttpContext.Current.Session["password"] != null)
{
string username = HttpContext.Current.Session["username"].ToString();
string password = HttpContext.Current.Session["password"].ToString();
string actionName = filterContext.ActionDescriptor.ActionName;
string controllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;
if (username == null || username == "" || password == null || password == "")
{
filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new
{
controller = "User",
action = "LogOut",
}));
//base.OnActionExecuting(filterContext);
}
else
{
//filterContext.HttpContext.Response.Redirect("~/"+controllerName+"/"+actionName);
//return;
//filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new
//{
// controller = controllerName,
// action = actionName,
// })); ...Removed this redirection and it worked for me.
base.OnActionExecuting(filterContext);
}
}
else
{
filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new
{
controller = "User",
action = "LogOut",
}));
//base.OnActionExecuting(filterContext);
}
base.OnActionExecuting(filterContext);
//}
//catch(Exception ex)
//{
// throw ex;
//}
}
}
I am trying to use it with session created with user login and without login it redirects to login page but after successfull login when I try to redirect to Index methhod of another controller it causes infinite loop of redirect, giving error in browser "redirected too many times."
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
