开发者

How do I track down ValidateAntiForgeryToken not supplied issue?

I was looking at our logs and came across an error:

A required anti-forgery token was not supplied or was invalid.

It seems like one of the developers may have not added the token onto a page or did not send it via an AJAX call. The problem is I have no idea where in our code base this originated. It was logged by the [HandleError] logging code we added but we have no way of knowing which method caused this.

The stack trace only shows us the following which doesn't seem very helpful:

at System.Web.Helpers.AntiForgeryWorker.Validate(HttpContextBase context, Stri开发者_Python百科ng salt) at System.Web.Mvc.ControllerActionInvoker.InvokeAuthorizationFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor) at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)

The OnException method in our BaseController looks as follows:

protected override void OnException(ExceptionContext filterContext)
{
    Exception ex = filterContext.Exception;

    //
    // Custom logging code here was removed for brevity

    if (filterContext.Exception.Data.Contains("Description") == false)
        filterContext.Exception.Data.Add("Description", "Oops. Something went wrong!");

    //Displays a friendly error, doesn't require HandleError
    filterContext.ExceptionHandled = true;

    //Displays a friendly error, *requires* HandlError
    base.OnException(filterContext);
}

My question is:

Is there a way to get the source of the exception, ie. to know what controller or source file threw the exception using ExceptionContext.

Appreciate the help.


Based on BuildStarted's response, the answer lies in the RouteData property of filterContext. I have an object dumper that writes out the properties of any object and here is what I see in our logs now.

RouteData:

[0]: [controller, Assess]

[1]: [action, Setup]

[2]: [id, 2]

So now I know the exact method that caused this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜