Question

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!

Was it helpful?

Solution

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.

OTHER TIPS

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.

What about using just Request.Form in your custom Authorization class. It is just a POST call after all? Don't make it harder than it actually is.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top