Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9107441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T02:34:20+00:00 2026-06-17T02:34:20+00:00

Is there a way to ensure ASP.NET MVC 4 forms are protected against CSRF

  • 0

Is there a way to ensure ASP.NET MVC 4 forms are protected against CSRF by default?

For instance, is there a way to have AntiForgeryToken automatically applied to all forms in both views and controller actions?

Background on this question: Prevent Cross-Site Request Forgery (CSRF) using ASP.NET MVC’s AntiForgeryToken() helper and Anatomy of a Cross-site Request Forgery Attack.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T02:34:21+00:00Added an answer on June 17, 2026 at 2:34 am

    To add to osoviejo’s excellent answer, the instructions below, from my recent blog post on CSRF, put his work together with the information in Phil’s blog in one comprehensive answer.

    ASP.NET/MVC provides a mechanism for this: you can add to to a collection of filters on the global FilterProviders object. This allows you to target some controllers and not others, adding the needed security feature.

    First, we need to implement an IFilterProvider. Below, you can find Phil Haack’s Conditional Filter Provider class. Begin by adding this class to your project.

    public class ConditionalFilterProvider : IFilterProvider
    {
        private readonly
          IEnumerable<Func<ControllerContext, ActionDescriptor, object>> _conditions;
    
        public ConditionalFilterProvider(
          IEnumerable<Func<ControllerContext, ActionDescriptor, object>> conditions)
        {
            _conditions = conditions;
        }
    
        public IEnumerable<Filter> GetFilters(
            ControllerContext controllerContext,
            ActionDescriptor actionDescriptor)
        {
            return from condition in _conditions
                   select condition(controllerContext, actionDescriptor) into filter
                   where filter != null
                   select new Filter(filter, FilterScope.Global, null);
        }
    }
    

    Then, add code to Application_Start that adds a new ConditionalFilterProvider to the global FilterProviders collection that ensures that all POST controller methods will require the AntiForgeryToken.

    IEnumerable<Func<ControllerContext, ActionDescriptor, object>> conditions = 
        new Func<ControllerContext, ActionDescriptor, object>[] {
        // Ensure all POST actions are automatically 
        // decorated with the ValidateAntiForgeryTokenAttribute.
    
        ( c, a ) => string.Equals( c.HttpContext.Request.HttpMethod, "POST",
        StringComparison.OrdinalIgnoreCase ) ?
        new ValidateAntiForgeryTokenAttribute() : null
    };
    
    var provider = new ConditionalFilterProvider(conditions);
    
    // This line adds the filter we created above
    FilterProviders.Providers.Add(provider);
    

    If you implement the two pieces of code above, your MVC application should require the AntiForgeryToken for every POST to the site. You can try it out on Phil Haack’s CSRF example web site – once protected, the CSRF attack will throw System.Web.Mvc.HttpAntiForgeryException without having to add the [ValidateAntiForgeryToken] annotation. This rules out a whole host of “forgetful programmer” related vulnerabilities.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to ensure that visitors of my ASP.NET MVC website always have the
I have a web app written in ASP.NET MVC 3.0. There are some largish
I'm wondering if there is a way to ensure that an ASP.NET application can
I have an ASP.NET MVC application that utilizes NHiberante and SQL Server 2008 on
I have a view model in asp .net mvc 3 which has IEnumerable<HttpPostedFileBase> files
I’m having problems with the AntiForgeryToken in ASP.Net MVC. If I do an iisreset
I have heard that you can run an ASP.NET application and ASP.NET mvc application
I am trying to ensure that all URLs used to access my ASP.NET MVC
In ASP.NET MVC I have a controller that looks somehow like this: public class
Is there any way to ensure that a class posts a particular NSNotification? (I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.