tags:

views:

38

answers:

1

Hi,

i'm trying to create an attribute that i can set on my controllers action in which i can check if a user in some kind of role. I know with standard asp.net authentication this is already possible, but i'm not using that.

So what i want to accomplish is this:

[Role("Admin", "SuperUser")]
public ActionResult SafeCourse()

I've constructed this:

public class AdminAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {

Ans now..... i want to redirect the user to some controller/action or if that isn't possible to some view when he / she is not in the right role. But i'm not in my usual context (= controller) and i'm somewhat lost :)

Michel

+1  A: 

If you make your action filter implement IAuthorizationFilter it will execute before other types ... then in OnAuthorization(AuthorizationContext filterContext) set the result.

Something like

filterContext.Result = new RedirectResult("/Controller/ActionYouWantToDirectTo");

Kindness,

Dan

Daniel Elliott
Thanks! that did the trick
Michel