0

I need your help.

I have read about the class attributes in C# and I understood that we use them for authorization, authentication or to get some information about the class when you use reflection in the reverse process.

but really I want to understand how the attributes of authentication and authorization work and how really they can force the user to follow some restrictions when we just put the attribute above the class and we do not do any other thing, I cant understand the flow of the authentication or the authorization process using class attributes.

May be my question is not clear enough or has some mistakes but really I need some body to explain for me the authentication and the authorization process using class attributes in C#.

Clear example would be appreciated.

Thanks Every Body.

Mo Haidar
  • 3,748
  • 6
  • 37
  • 76
  • Are you talking about the AuthorizeAttribute class in System.Web.Mvc? – Brian Driscoll Dec 12 '14 at 17:45
  • @BrianDriscoll MVC or suppose that I want to create a costume attribute to do the same thing that the MVC class attributes do, how that could be done. – Mo Haidar Dec 12 '14 at 17:48
  • there's a lot guides out there, google returns a lot of hits for `custom authorize attribute mvc`. That's where you should start – Jonesopolis Dec 12 '14 at 17:49
  • @Mohammad without more details from you about the context in which you are trying to authorize access to some resource I can't really explain it to you... it'd be like trying to boil the ocean. – Brian Driscoll Dec 12 '14 at 17:50
  • @BrianDriscolljust I want to understand how these attribute work, suppose that we want to use the attribute [Authorize(Roles="Administrators")] above a class named Admin, now how the MVC works to prevent the non admin users from using this class. if you see that I should read something to understand this process please just guide me. I am new to MVC :(. – Mo Haidar Dec 12 '14 at 17:56

2 Answers2

3

There are reflection libraries that let you get the attributes on a particular class and iterate over them.

once you understand how attribute values and properties can be iterated over with reflection then it's not too much of a stretch to conceptually understand how they can be used for validation.


You can also use reflection to iterate over methods and properties of an object, and invoke those methods/properties. Microsoft has some pretty good documentation out there for this, so if you want to look this up, you can just goggle it.


Here's a sample program. making use of attributes

class Program
{
    static void Main(string[] args)
    {
        var something = new ClassWithAttributes();
        var attributes = typeof(ClassWithAttributes).GetCustomAttributesData();

        var attribute = (SomeAttribute) Attribute.GetCustomAttribute(typeof(ClassWithAttributes), typeof (SomeAttribute));

        Console.WriteLine(attribute.Name);
        Console.ReadKey(false);
    }
}

[Some("larry")]
class ClassWithAttributes
{

}

public class SomeAttribute : System.Attribute
{
    public string Name { get; set; }

    public SomeAttribute(string name)
    {
        this.Name = name;
    }
}

and here's the documentation that I used to help me make that sample

http://msdn.microsoft.com/en-us/library/sw480ze8.aspx

http://msdn.microsoft.com/en-us/library/71s1zwct%28v=vs.110%29.aspx

1

Attributes apply functionality to a class by means of Reflection. The class can get the attributes it is adorned with, and use them and any parameters as needed.

Further reading: Attributes Tutorial (MSDN)

Gigi
  • 28,163
  • 29
  • 106
  • 188