title: Building CustomAuthorization in ASP.NET MVC
body: <p>In the <code>DB</code> i have <code>Role</code> and <code>User</code> entities with one to many relationship.</p>
  
  <p>What i am trying to do is to build custom authorization filter. All the tutorials that i have seen are using default <code>ASP.NET</code> membership. All i know is that i need to inherit <code>AuthorizationAttribute</code> but do not know which methods do i need to override and how to implement them.</p>
  
  <pre><code>public class UserAuth : AuthorizeAttribute
  {
  
  }
  </code></pre>
  
  <p>In the <code>DB</code>:</p>
  
  <p><strong>Role</strong></p>
  
  <pre><code>public class Role
  {
      [Key]
      public int RoleID { get; set; }
  
      [Required]
      public int RolenameValue { get; set; }
  
      [MaxLength(100)]
      public string Description { get; set; }
  
      // // // // //
  
      public Rolename Rolename 
      {
          get { return (ProjectName.Domain.Enums.Rolename)RolenameValue; }
          set { RolenameValue = (int)value; }
      }
  
      public virtual ICollection&lt;User&gt; Users { get; set; }
  }
  </code></pre>
  
  <p><strong>User</strong></p>
  
  <pre><code>public class User
  {
      [Key]
      public int UserID { get; set; }
  
      [Required]
      [MaxLength(30)]
      public string Username { get; set; }
  
      [Required]
      [MinLength(5)]
      public string Password { get; set; }
  
      [Required]
      [DataType(DataType.EmailAddress)]
      public string Email { get; set; }
  
      [MaxLength(30)]
      public string FirstName { get; set; }
  
      [MaxLength(50)]
      public string LastName { get; set; }
  
      [DataType(DataType.Date)]
      public DateTime Birthdate { get; set; }
  
      public int GenderValue { get; set; }
  
      // // // // // // //
  
      public Gender Gender
      {
          get { return (ProjectName.Domain.Enums.Gender)GenderValue; }
          set { GenderValue = (int)value; }
      }
  
      public int RoleID { get; set; }
  
      [ForeignKey("RoleID")]
      public Role Role { get; set; }
  </code></pre>