asp.netoverridingdotnetnukeauthorize

ASP.Net and DotNetNuke -- why does a customer AuthorizeAttribute override ValidateAntiForgeryToken?


I have a custom AuthorizationAttribute:

public class CodeWomplerAuthorizeAttribute : AuthorizeAttribute
{
    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        return SessionManager.CheckSession(SessionKeys.User)==true;
    }

    protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
    {
        if (SessionManager.CheckSession(SessionKeys.User) == false)
        {
            filterContext.Result = new RedirectToRouteResult(
                new RouteValueDictionary 
                {
                    { "action", "ActionName" },
                    { "controller", "ControllerName" }
                });
        }
        else
            base.HandleUnauthorizedRequest(filterContext);
    }
}

And here is an example function in the controller:

    [HttpPost]
    [DotNetNuke.Web.Mvc.Framework.ActionFilters.ValidateAntiForgeryToken]
    [CodeWomplerAuthorize]
    public string InitializeNew()
    {
        var techSheet = new TechSheet {WorkOrder = {CreateDate = DateTime.Now}};
        var empList = new WorkOrderEmployeeController().Gets().Recordset;
        return JsonConvert.SerializeObject(Json(new{techSheet,empList}));
    }

My custom attribute works great. But, if I include [DotNetNuke.Web.Mvc.Framework.ActionFilters.ValidateAntiForgeryToken], I get a failure.

ValidateAntiForgeryToken fails if AuthorizeCore has an override.

How do I include ValidateAntiForgeryToken in the override of AuthorizeCore?


Solution

  • Here is one way:

    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        var cookie = httpContext.Request.Cookies[AntiForgeryConfig.CookieName];
        AntiForgery.Instance.Validate(cookie?.Value, httpContext.Request.Headers["__RequestVerificationToken"]);` protected override bool AuthorizeCore(HttpContextBase httpContext)
        return SessionManager.CheckSession(SessionKeys.User)==true;
    }