Cybercrux

Everything is achievable through technology

Controller and Action Name

Controller and Action name inside a class

public new HttpContextBase httpContext
{
get
{
HttpContextWrapper context =
new HttpContextWrapper(System.Web.HttpContext.Current);
return (HttpContextBase)context;
}
}
public override bool IsUserInRole(string username, string roleName)
{
//var routeValueDictionarys =
string url = HttpContext.Current.Request.RawUrl;
RouteData route = RouteTable.Routes.GetRouteData(httpContext);
UrlHelper urlHelper = new UrlHelper(new RequestContext(httpContext, route));
var routeValueDictionary = urlHelper.RequestContext.RouteData.Values;
string controllerName = routeValueDictionary["controller"].ToString();
string actionName = routeValueDictionary["action"].ToString();
RouteData rd = HttpContext.Current.Request.RequestContext.RouteData;
string currentController = rd.GetRequiredString("controller");
string currentAction = rd.GetRequiredString("action");
throw new NotImplementedException();
}

inside Attribute


class RoleGroupFilterAttribute : ActionFilterAttribute
{
public RoleType GroupName { get; set; }
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
string[] users = new string[] { };
string[] roles = new string[] { };
// To Do: get the roles / users for the group from your db or storage
//var Controllers = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;
var Controllers = filterContext.RouteData.Values["controller"];
// routeData.Values["action"];
var Actiosn = filterContext.ActionDescriptor.ActionName;
var IP = filterContext.HttpContext.Request.UserHostAddress;
var DateTime = filterContext.HttpContext.Timestamp;
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();
}
}
}

Advertisements

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