Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save juloliveira/155d416b83da1af7dce4b76de7bb6de8 to your computer and use it in GitHub Desktop.
Save juloliveira/155d416b83da1af7dce4b76de7bb6de8 to your computer and use it in GitHub Desktop.
using System.Security.Claims;
using System.Web.Mvc;
using System.Web.Routing;
namespace Bandeirantes.OpenIDConnect
{
public class ClaimsAuthorizeAttribute: AuthorizeAttribute
{
private string claimType;
private string claimValue;
public ClaimsAuthorizeAttribute(string type, string value)
{
this.claimType = type;
this.claimValue = value;
}
public override void OnAuthorization(System.Web.Mvc.AuthorizationContext filterContext)
{
var user = filterContext.HttpContext.User as ClaimsPrincipal;
if (!user.Identity.IsAuthenticated)
base.HandleUnauthorizedRequest(filterContext);
else if (user != null && user.HasClaim(claimType, claimValue))
base.OnAuthorization(filterContext);
else
{
filterContext.Result = new RedirectToRouteResult(
new RouteValueDictionary(new
{
controller = "oops",
action = "sempermissao"
})
);
return;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment