You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AuthFilter.cs 1.1KB

12345678910111213141516171819202122232425262728293031323334353637
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Web.Http;
  4. using System.Web.Http.Controllers;
  5. namespace iiie.Authentication.Business
  6. {
  7. /// <summary>
  8. /// Filter for controllers methods
  9. /// </summary>
  10. public class AuthFilter : AuthorizeAttribute
  11. {
  12. /// <summary>
  13. /// Authorized roles to access this method
  14. /// </summary>
  15. public IEnumerable<int> UserRoles { get; set; }
  16. /// <summary>
  17. /// Constructor
  18. /// </summary>
  19. /// <param name="roles">The authorized roles</param>
  20. public AuthFilter(params int[] roles)
  21. {
  22. UserRoles = roles.ToList();
  23. }
  24. /// <summary>
  25. /// Check if user can access this method
  26. /// </summary>
  27. /// <param name="context">HTTP request context</param>
  28. /// <returns>True if user can access, false otherwise</returns>
  29. protected override bool IsAuthorized(HttpActionContext context)
  30. {
  31. return !UserRoles.Any() || (UserStorage.BasicUserDbo != null && UserRoles.Contains(UserStorage.BasicUserDbo.Role));
  32. }
  33. }
  34. }