Skip to content

Instantly share code, notes, and snippets.

@arc95
Last active May 16, 2017 18:01
Show Gist options
  • Save arc95/b3061a34c471feea6a5e5021026d2dea to your computer and use it in GitHub Desktop.
Save arc95/b3061a34c471feea6a5e5021026d2dea to your computer and use it in GitHub Desktop.
public class AuthorizeUserAttribute : AuthorizeAttribute
{
public string AccessLevel { get; set; }
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
var isAuthorized = base.AuthorizeCore(httpContext);
if (!isAuthorized)
{
return false;
}
var privilegeLevels = GetAuthorizedRoles(); // Call another method to get rights of the user from DB
if (privilegeLevels.Contains(this.AccessLevel))
{
return true;
}
else
{
return false;
}
}
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
base.HandleUnauthorizedRequest(filterContext);
filterContext.Result = new RedirectResult("~/Home/Unauthorized");
}
private IEnumerable<string> GetAuthorizedRoles()
{
var roles = "Goodbye";
return roles.Split(',');
}
}
[AuthorizeUser(AccessLevel = "Hello")]
public ActionResult Index()
{
return View();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment