Cybercrux

Everything is achievable through technology

MVC

Model View Controller

 

Controller Action with Role and Privileges

public class RoleGroupFilterAttribute : ActionFilterAttribute
{
public string GroupName { get; set; }

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
string [] users = new string[] {};
string [] roles = new string[] {};

// get the roles / users for the group from your db or storage

var user = filterContext.HttpContext.User;
var userRoles = Roles.GetRolesForUser();

if (!roles.Any(r => userRoles.Contains(r)) && !users.Contains(user.Identity.Name))
{
// return a not found result or redirect to an action
filterContext.Result = new HttpNotFoundResult();
}
}
}

[RoleGroupFilter(GroupName="MyGroup")]
public ActionResult Index()
{
return View();
}

2

public class CustomAuthorizeAttribute : AuthorizeAttribute
{
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
var userIdentity = httpContext.User.Identity;

if (!userIdentity.IsAuthenticated)
return false;

var rd = httpContext.Request.RequestContext.RouteData;
string currentAction = rd.GetRequiredString("action");
if(currentAction == "SalesIndex")
{
return IsUserIsInRoleForTheView(userIdentity.Name);
}

return true;
}
}

[CustomAuthorize]
public ActionResult SalesIndex()
{
return View();
}

One thought on “MVC

  1. View Bag As Model

    @{var ActivePrivilege = ViewBag.ActivePrivileges as IEnumerable;}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s