0

I have an MVC application what restricts the user from accessing controller actions based on a value stored in the session.

I have implemented as follows:

public partial class MyBookingController : CruisesDesktopControllerBase
{
    private bool CheckLoggedIn()
    {
        return MyBookingSessionInfo.OzBookingId > 0;
    }

    public virtual ActionResult Summary()
    {
        //Ensure user is logged in
        if (!CheckLoggedIn())
            return RedirectToAction(MVC.MyBooking.Login());

        //Prepare the view model
        SummaryViewModel summaryViewModel = new SummaryViewModel
                                            {
                                                OzBookingId = MyBookingSessionInfo.OzBookingId
                                            };

        return View(summaryViewModel);
    }

}

So instead of doing the if test at the top of controller actions I want to protect, is there a way to do this where the controller action could be annotated in some way to enforce the "logged in restriction" and hence removing the if test block?

TheEdge
  • 9,291
  • 15
  • 67
  • 135
  • Possible duplicate of [Override Authorize Attribute in ASP.NET MVC](http://stackoverflow.com/questions/746998/override-authorize-attribute-in-asp-net-mvc) – Steve Apr 11 '16 at 00:41
  • Yes, using the `[Authorize]` attribute. –  Apr 11 '16 at 00:43
  • @StephenMuecke [Authorize] assumes use of the MS authentication pipeline which in this case I am not. – TheEdge Apr 11 '16 at 00:45

2 Answers2

3

You can create a custom AuthorizeAttribute and override AuthorizeCore method.

public class AuthorizeUserAttribute : AuthorizeAttribute
{
    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        return /* custom logic */;          
    }

    protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
    {
        if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
        {
            filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { controller = "Error", action = "AccessDenied" }));
        }
    }
}

Then add this attribute on your action.

[AuthorizeUser]
public virtual ActionResult Summary()
BrunoLM
  • 97,872
  • 84
  • 296
  • 452
0

You can inherit the AuthorizeAttribute and then override the following methods AuthorizeCore(HttpContextBase) - authorization logic
HandleUnauthorizedRequest(AuthorizationContext) - Logic when authorization fails

public class CheckLoggedInAttribute : AuthorizeAttribute
{
    protected override bool AuthorizeCore(HttpContextBase httpContext) 
    {
        //Authorization logic Here. You can access the session using httpContext. Return false if your authorization fails
    }

    protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
    {
        //Logic when authorization fails, modify the ViewResult or something.
        base.HandleUnauthorizedRequest(filterConext)
    }
}
Bon Macalindong
  • 1,310
  • 13
  • 20