I have the following requirement for implementing an access control list
public class SecurityObject{ public string Key{get;set;} public string DisplayName{get;set;} public bool isAllowed{get;set;} } public class Role{ List<SecurityObject> AccessibleObjects{get;set;} }
I am currently using authentication for basic authorization. Below is my code
Global.asax.cs
public class MvcApplication : System.Web.HttpApplication { public override void Init() { this.PostAuthenticateRequest += new EventHandler(MvcApplication_PostAuthenticateRequest); base.Init(); } void MvcApplication_PostAuthenticateRequest(object sender, EventArgs e) { HttpCookie authCookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName]; if (authCookie != null) { string encTicket = authCookie.Value; if (!String.IsNullOrEmpty(encTicket)) { FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(encTicket); string[] userData = ticket.UserData.Split(new string[] { "___" }, StringSplitOptions.None); string[] roles = null; if (userData.Length > 1) { roles = userData[1].Split(','); } MyCustomIdentity identity = new MyCustomIdentity(ticket); GenericPrincipal principle = new GenericPrincipal(identity, roles); HttpContext.Current.User = principle; } } }}
My current controller class
public class AdminController : Controller { [HttpPost, Authorize, ValidateAntiForgeryToken] public ActionResult SaveUser(UserDetailViewModel viewModel) { } }
Class of my target controller
public class AdminController : Controller { [HttpPost, Authorize(ACLKey="USR_SAVE"), ValidateAntiForgeryToken] public ActionResult SaveUser(UserDetailViewModel viewModel) { } }
I want my action method to be decorated with ACLKey, and I would like to check if the user role has the given key and based on the fact that I need to execute or return the HttpUnauthorizedResult page, even for Ajax requests from jQuery.
I mentioned many such as Configuring Authentication in ASP.NET MVC But I did not find a way to authenticate both forms and my regular ACLKey.
How to parse USR_SAVE value and handle user authentication using CustomAuthorizeFilter?