How can I change the default authentication redirection path (/Account/Login) for controllers ? e.g I got 4 controllers
ABC --> /ABC/Login
BCD --> /BCD/Login
EFG --> /EFG/Login
Home ---> Account/Login
Here is a custom Authorize attribute, as per Chris Pratt's idea:
public class CustomAuthorize:AuthorizeAttribute
{
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
string controller = filterContext.RouteData.Values["controller"].ToString();
filterContext.Result = new RedirectToRouteResult(new
RouteValueDictionary(new{ controller = controller, action = "Login" }));
}
}
Can be used on your controller like this:
[CustomAuthorize]
public class ABCController : Controller
This will redirect an unauthorized client to the Login action on the controller it's trying to access. Remember to put [AllowAnonymous]
on your Login actions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With