Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8435387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:54:04+00:00 2026-06-10T06:54:04+00:00

I have function which return true or false depend of UserID and User Role.

  • 0

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 ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T06:54:06+00:00Added an answer on June 10, 2026 at 6:54 am

    You could write a custom RoleProvider inheriting from the default one and override the GetRolesForUser method:

    public class CachingRoleProvider: THE_ROLE_PROVIDER_YOU_ARE_CURRENTLY_USING
    {
        public override string[] GetRolesForUser(string username)
        {
            string[] roles;
            if (TryCheckFromYourCacheIfUserIsInRole(username, out roles))
            {
                // the roles for this user were retrieved from the cache
                return roles;
            }
    
            // no roles retrieved from the cached => query the base role provider
            roles = base.GetRolesForUser(username);
    
            // Store the retrieved roles into the cache so that on subsequent calls
            // you no longer need hit the base role provider for this user
            PutRoleInCacheForUser(username, roles);
    
            return roles;
        }
    }
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function func which returns true or false . Until func returns
I have a function is_prime(n) which returns True if n is prime and False
I have a function which checks the passed value and returns false or true,
I have a controller function which return a response with a value, and I
Is it possible to have the return of a JS function depend on user
Let's say I have the following function which should return True if valid and
I have a small method which return true or false if a timestamp is
Can I´m asking for advice. I have function which should return javascript object function
hi i have function which is called by tinker listbox so i cannot return
I have a function which returns 3 numbers, e.g.: def numbers(): return 1,2,3 usually

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.