Advertisement
Guest User

AuthorizeForRoleAttribute

a guest
Dec 6th, 2013
657
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.43 KB | None | 0 0
  1. public class AuthorizeForRoleAttribute : AuthorizeAttribute
  2.     {
  3.         /// <summary>
  4.         /// Injected application context
  5.         /// </summary>
  6.         /// <value>
  7.         /// The application context.
  8.         /// </value>
  9.         [Inject]
  10.         public IAppContextProvider appContext { get; set; }
  11.  
  12.         /// <summary>
  13.         /// Processes requests that fail authorization.
  14.         /// </summary>
  15.         /// <param name="actionContext">The context.</param>
  16.         protected override void HandleUnauthorizedRequest(System.Web.Http.Controllers.HttpActionContext actionContext)
  17.         {
  18.             base.HandleUnauthorizedRequest(actionContext);
  19.         }
  20.  
  21.         /// <summary>
  22.         /// Indicates whether the specified control is authorized.
  23.         /// </summary>
  24.         /// <param name="actionContext">The context.</param>
  25.         /// <returns>
  26.         /// true if the control is authorized; otherwise, false.
  27.         /// </returns>
  28.         /// <exception cref="System.NullReferenceException">Failed to inject application context provider</exception>
  29.         protected override bool IsAuthorized(System.Web.Http.Controllers.HttpActionContext actionContext)
  30.         {
  31.             if (appContext == null)
  32.                 throw new NullReferenceException("Failed to inject application context provider");
  33.  
  34.             return appContext.IsInRole(HttpContext.Current.User.Identity.Name, this.Roles);
  35.         }
  36.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement