views:

50

answers:

1

I wrote an extension class to customize my AuthorizeAttribute for my action methods and I'd like to be able to inject messages into my view when a certain condition is met. I"m using the below code to load up a shared view when a user is not authorized but it's not adding my message to my ViewData collection. Any ideas?

public override void OnAuthorization(AuthorizationContext filterContext)
{
    base.OnAuthorization(filterContext);
    if (IsNotAuthorized)
    {
        filterContext.Result = new ViewResult { ViewName = "NotAuthorized" };
        filterContext.Controller.ViewData["Message"] = "Go Away";
    }
}

I've also tried setting my ViewData["Message"] collection item above the call to change the view with no success.

A: 

Have you tried;

filterContext.Result = new RedirectResult("Home/Index");

I don't know how to add the ViewData but this will get you to the not authorised controller at least.

I'll keep looking for code to add to view data in the mean time or until someone posts it.

edit

This may help;

Changing ActionExecutingContext values in Custom Filter Attribute

griegs
hmm..the solution you linked appears to be for something else. Anyone else have any other ideas?
Kyle