The name 'AntiForgery' does not exist in the current context

Viewed 2687

I am Trying to implement Antifrogery Token in my Web Api but getting following error:

The name 'AntiForgery' does not exist in the current context.

Here is My code:

using System.Threading;
using System.Threading.Tasks;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;
namespace MyAPI
{
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public sealed class ValidateAntiForgeryTokenAttribute: FilterAttribute
{

    public Task<HttpResponseMessage> ExecuteAuthorizationFilterAsync(HttpActionContext actionContext, CancellationToken cancellationToken, Func<Task<HttpResponseMessage>> continuation)
    {
        try
        {
            string cookieToken = "";
            string formToken = "";
            //I am getting error on the line written below for  "AntiForgery":
            AntiForgery.Validate(cookieToken, formToken);
        }
        catch
        {
            actionContext.Response = new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.Forbidden,
                RequestMessage = actionContext.ControllerContext.Request
            };
            return FromResult(actionContext.Response);
        }
        return continuation();
    }

    private Task<HttpResponseMessage> FromResult(HttpResponseMessage result)
    {
        var source = new TaskCompletionSource<HttpResponseMessage>();
        source.SetResult(result);
        return source.Task;
    }
 }
}
2 Answers
  1. Add namespace "using System.Web.Helpers;".
  2. Install package "Microsoft.AspNet.WebPages" in the Nuget packages.
  3. Add Reference "System.Web" to the project.
Related