Advertisement
Willcode4cash

Restrict access to method to a user role.

Aug 10th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.52 KB | None | 0 0
  1. namespace MyApps.Web.Models
  2. {
  3.     using System;
  4.     using System.Web;
  5.     using System.Web.Mvc;
  6.  
  7.     [AttributeUsage(AttributeTargets.Method)]
  8.     public class AjaxUserAttribute : ActionFilterAttribute
  9.     {
  10.         public override void OnActionExecuting(ActionExecutingContext filterContext)
  11.         {
  12.             if (HttpContext.Current.User.IsInRole("User")) return;
  13.             filterContext.HttpContext.Response.StatusCode = 401;
  14.             filterContext.Result = new HttpUnauthorizedResult();
  15.         }
  16.     }
  17. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement