Posts

Adding ASP.NET MVC Anti-Forgery Tokens To All Post Requests Globally

Image
This post is how to implement anti forgery validation with ASP.NET MVC. The anti-forgery token can be used to help protect your application against cross-site request forgery. To use this feature, call the AntiForgeryToken method from a form and add the ValidateAntiForgeryTokenAttribute attribute to the action method that you want to protect. It'll be always good to avoid repetitive coding, especially when the framework is flexible enough to avoid it. Below is my solution to to create a flexible solution to validate all post operations.   Next step will be to register the above filter globally with GlobalFilterCollection in Global.asax: All of our post operations are now checked for forgery; however, this will fail because we haven’t added our token globally. To enable AntiForgeryToken in client side, I added a   @Html.AntiForgeryToken()   element in the   Index.cshtml   file. You can do it   _layout.cshtml   as well. This will render the...

Why 'this' keyword cannot be used in a static method

this is used to refer to the parent object of a variable or method. When you declare static  on a method the method can be called without needing to instantiate an object of the class. Therefore the  this keyword  is not allowed because your  static  method is not associated with any objects.

Indexer in C#

An  indexer  allows an object to be indexed such as an array. When you define an indexer for a class, this class behaves similar to a  virtual array . You can then access the instance of this class using the array access operator ([ ]). In other words it simplifies the way we access the collection. using System ; namespace IndexerApplication { class IndexedNames { private string [] namelist = new string [ size ]; static public int size = 10 ; public IndexedNames () { for ( int i = 0 ; i < size ; i ++) namelist [ i ] = "N. A." ; } public string this [ int index ] { get { string tmp ; if ( index >= 0 && index <= size - 1 ) { tmp = namelist [ index ]; } else { tmp = "" ; } ...