While I think that you should change approach to solution about authorization, you can stay with it and use this code:
public partial class BaseUserAwareController : Controller
{
protected override void OnAuthorization(AuthorizationContext filterContext)
{
if (GetCurrentUser() == null)
{
filterContext.Result = new HttpUnauthorizedResult();
}
}
}
If your project is not too big, think about changing it to use [Authorize]
. If you used it, it would be only:
[Authorize]
public partial class UserAwareController : Controller
{
}
You may think that this is not that big difference, but [Authorize]
handles also some caching issues (returning cached response when you are not authorized anymore).
Install MVC 2 and create new MVC 2 web application. It contains authorization logic that you can propably use in your application.