I would like to redirect visitors to a login page with added parameters (based on the action they are performing) after the authorization fails.
This is an example of what I would like to do:
ASP.NET MVC - CustomeAuthorize filter action using an external website for loggin in the user
However, since this is a custom filter, I do not know how or if I can specify the Roles like in the usual authorization filter. I would like something like:
[CustomAuthorization(Roles="Admins")]
Thank you!
You could inherit from AuthorizeAttribute class and override OnAuthorize method like this:
public override void OnAuthorization(AuthorizationContext filterContext)
{
base.OnAuthorization(filterContext);
if (!HttpContext.Current.User.IsAuthenticated)
{
filterContext.Result = new RedirectResult("target");
}
}
Then you can use this custom filter just like AuthorizeAttribute.