In a web app I am trying to introduce action attributes to add authentication in my actions. Right now I individually check for a valid session in each action.
I have created a custom attribute which uses AuthorizeAttribute:
public class BaseAuthAttribute : AuthorizeAttribute
And I decorate my actions with
[BaseAuth]
Now in the BaseAuthAttribute I have this code
public override void OnAuthorization(AuthorizationContext filterContext)
{
var session = new BusinessLayer.PortalUser(filterContext.HttpContext.Request.Cookies["appname"]);
if(!session.IsAuthorized()
{
filterContext.Result = new RedirectToRouteResult(
new RouteValueDictionary { { "controller", "Home" }, { "action", "Login" } });
}
}
But when I do not have an active session, the Result = new line blows up with an object not sense to an instance of an object.
I am not using ASP.Net built in auth, but rather doing custom to determining if there is a session/user. So is filterContext only to be used when you are using the AP.Net membership classes?
I need to redirect to a view if their session is expired/non-existent, or indeed if they do have the correct permissions