Willcode4cash icon

Restrict access to method to a user role.

Willcode4cash | PRO | 08/11/16 12:00:29 AM UTC | 0 ⭐ | 265 👁️ | Never ⏰ | []
C# |

520 B

|

None

|

0 👍

/

0 👎

namespace MyApps.Web.Models
{
    using System;
    using System.Web;
    using System.Web.Mvc;
 
    [AttributeUsage(AttributeTargets.Method)]
    public class AjaxUserAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (HttpContext.Current.User.IsInRole("User")) return;
            filterContext.HttpContext.Response.StatusCode = 401;
            filterContext.Result = new HttpUnauthorizedResult();
        }
    }
}

Comments