views:

901

answers:

2

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:

http://stackoverflow.com/questions/835672/asp-net-mvc-customeauthorize-filter-action-using-an-external-website-for-loggin

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!

A: 

Have you tried downloading the ASP.Net MVC source and taking a look at the AuthorizeAttribute's code (in AutorizeAttribute.cs)?

It might make sense to derive your CustomAutorization from the existing AuthorizeAttribute - check it out and see if you can tack on your required functionallity.

Kevin Pullin
+1  A: 

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.

bbmud