What is the name of these parameters in ASP.NET MVC?

2

What is the name of these parameters in brackets used to restrict some access or define the protocol used, as in the example: [Authorize] and [HttpPost] .

Is it possible to create a custom "filter"? for example, to allow access to users with a specific Role level.

    
asked by anonymous 29.02.2016 / 22:50

2 answers

6

These are attributes , they are not parameters. This is part of C #. The attribute is just information, called metadata. He alone does nothing. There needs to be a mechanism in the code that reads it and does something.

The ones you see are not completely customizable. It was the ASP.Net MVC that created them. In this case they are called action filters .

You can create yours too, by following a few rules. Obviously there must be a reason to create one and have some mechanism that consumes them.

29.02.2016 / 22:59
1

You do not need to create a new attribute to limit authorization by ROLE. You can use Authorize itself, for example:

[Authorize(Roles="User")]

Now if you'd really like to create a new attribute, here's some information.

You can create a custom attribute as follows:

public class Author : System.Attribute
{
   private string name;
   public double version;

   public Author(string name)
   {
       this.name = name;
       version = 1.0;
   }
}

[Author("P. Ackerman", version = 1.1)]
class SampleClass
{
    // P. Ackerman's code goes here...
}

But the attribute alone does nothing. You need to create a routine that uses reflection to identify the attributes and give some meaning to them. Here is a reference link that explains it best.

Reference: link

    
29.02.2016 / 23:08