Jonathas Sucupira

Jonathas Sucupira

  • NA
  • 166
  • 15.9k

Help on how to use Attribute with custom classes

Jul 27 2011 12:18 PM
Hello,

I was wanting to use an attribute in some of my class methods that would make sure that the user is an authorized before using the method that they called.I was wanting to do something like
[Authorized()]
public void updateSomething()
{
//TODO:
}
Here is my attribute class

[
 class AuthorizedAttribute : Attribute     
{  

 public bool IsAuthorized { getset; }  

 public string UserName { getset; }  

 public string Password { getset; }  

 public string UserEmail { getset; }

 public AuthorizedAttribute()
     
{
         
//This is not the actual implementation
         
this.IsAuthorized = false;
     
}

   
public AuthorizedAttribute(string userEmail, string userPassword)
     
{
       
this.UserEmail = userEmail;
       
this.Password = userPassword;
       
this.UserName = string.Empty;

       
BusinessLogic bc = new BusinessLogic();
       
if (bc.VerifyCredentials(userEmail, userPassword))
       
{
           
this.IsAuthorized = true;
       
}
       
else
       
{
           
this.IsAuthorized = false;
       
}
   
}
}
Could someone point me in the right direction? Some link would be great as well.
Thank you.

Answers (3)