Skip to content

Instantly share code, notes, and snippets.

@aidangarnish
Created July 2, 2019 14:14
Show Gist options
  • Select an option

  • Save aidangarnish/ec87f007d167236a3c3d5ea7001b913e to your computer and use it in GitHub Desktop.

Select an option

Save aidangarnish/ec87f007d167236a3c3d5ea7001b913e to your computer and use it in GitHub Desktop.
public class AuthorizeADAttribute : AuthorizeAttribute { public string GroupId { get; set; }
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
if (base.AuthorizeCore(httpContext))
{
if (String.IsNullOrEmpty(GroupId))
return true;
var identity = (ClaimsPrincipal)Thread.CurrentPrincipal;
var claim = identity.Claims.Where(c => c.Value == GroupId).FirstOrDefault();
if(claim != null)
{
return true;
}
else
{
return false;
}
}
return false;
}
protected override void HandleUnauthorizedRequest(System.Web.Mvc.AuthorizationContext filterContext)
{
if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
{
base.HandleUnauthorizedRequest(filterContext);
}
else
{
filterContext.Result = new RedirectToRouteResult(
new System.Web.Routing.RouteValueDictionary(new { controller = "Home", action = "FailedLogin" }));
filterContext.Result.ExecuteResult(filterContext.Controller.ControllerContext);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment