First, you will need to create additional tables for advanced role management, such as projects
, as well as a relationship with users
in the context of operations
, which could be your controller actions
.
One way to do this is to create your own table for roles
. In this case, you will only use Asp net membership users
, but it all depends on your requirements.
Secondly, you need to process it in MVC
. In my opinion, the best way is to implement it using your own custom attribute Authorization
and decorate your actions with your authorization attribute instead of the [Authorization]
attribute.
It is very simple.
[CustomAuthorize]
To do this, you need to assign your class from FilterAttribute
, as well as implement the IAuthorizationFilter
interface.
public void OnAuthorization(AuthorizationContext filterContext) { HttpCookie authCookie = filterContext.HttpContext.Request.Cookies[FormsAuthentication.FormsCookieName]; if (authCookie != null) { FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value); var identity = new GenericIdentity(authTicket.Name, "Forms"); var principal = new GenericPrincipal(identity, new string[] { authTicket.UserData }); filterContext.HttpContext.User = principal; } var controller = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName; var action = filterContext.ActionDescriptor.ActionName; var user = filterContext.HttpContext.User; var ip = filterContext.HttpContext.Request.UserHostAddress; var isAccessAllowed = CustomAuthenticationLogic.IsAccessAllowed(controller, action, user, ip); if (!isAccessAllowed) {
In the OnAuthorization
method OnAuthorization
you can get all the information that may be required in your custom authorization logic, for example, HttpContext
, Controller
name, Action
name. You can simply call your authentication logic from this method. Your custom authentication logic might look like this.
public class CustomAuthenticationLogic { public static bool IsAccessAllowed(string controller, string action, IPrincipal user, string ip) {