I have function which return true or false depend of UserID and User Role.
I have controller with several action results.
for example
public class DemoController : Controller
{
public ActionResult Index(){}
public ActionResult Contact(){}
}
So i want , everytime when user use this actions, to check if user is in role.
I know i can make it like
[Authorize(Roles = "Administrator")]
public ActionResult JustAdmins(){}
But this kind of way, everytime user visit this action, its an extra SQL Query.
My want to store user role in MemCached so my function will be like
public static bool IsCompany(Guid UserID)
{
//if (get from cache != null && get from cache == "Role")
// return true
//if (get from DB != null && get from DB == "Role")
//return true
return false;
}
But how i can inherit all Action in my controller to check this function ?
tip: maybe override OnActionExecuting or similar ?
You could write a custom
RoleProviderinheriting from the default one and override the GetRolesForUser method:Obviously by doing this you completely acknowledge the fact that you could get out of sync if some external process modifies the roles in the datastore used by your base role provider since now you are reading the cached roles, not the ones from your datastore. So you might need to put some synchronization mechanisms in order to evict the cached data for the given username in this case.