Question

I've written custom role provider, which internally uses web service methods for getting roles or usernames. This provider inherits from System.Web.Security.RoleProvider. In the web.config file I switched on .NET provided caching feature which uses cookies.

Here is how looks web.config section for this provider:

<system.web>      
      <roleManager defaultProvider="MyProvider"
                 enabled="true"
                 cacheRolesInCookie="true"
                 cookieName=".MYROLES"
                 cookieTimeout="30"
                 cookiePath="/"
                 cookieRequireSSL="false"
                 cookieSlidingExpiration="true"
                 cookieProtection="All">
        <providers>
          <clear/>
          <add name="MYProvider"
               type="MYProvider.MyRoleProvider, MYProvider"
               Service1URL="http://localhost:54013/service1.asmx"
               Service2URL="http://localhost:54013/service2.asmx"
               rolesPrefix="ABC_"
               domainName="abc.corp"
               specialUserForAllRoles="abc"
               applicationURL="http://example.com"
               logCommunication="true"/>
        </providers>
      </roleManager>
</system.web>

Now, it comes to test if the cache is working or not. I've written simple method which looks like that:

public void TestCache()
{
   string[] roles = Roles.GetAllRoles();
   roles = Roles.GetAllRoles();

   string[] rolesForUser1 = Roles.GetRolesForUser("user1");
   rolesForUser1 = Roles.GetRolesForUser("user1");

   string[] usersInRole = Roles.GetUsersInRole("ABC_DEV");
   usersInRole = Roles.GetUsersInRole("ABC_DEV");       

   Roles.IsUserInRole("user1", "ABC_DEV");
   Roles.IsUserInRole("user1", "ABC_DEV");
}

While debugging this piece of code (from test web site), debugger enters each of shown method in provider and executes all logic inside, despite the fact method induction is redundant or not. I thought that second invoke of each method should not execute because the result will be returned without asking of my provider directly from cache.

What I'm doing/thinking wrong and how to fix the caching feature ?

Regards

Was it helpful?

Solution

The role cache works only for roles of the current user. This should be cached:

var isInRole = User.IsInRole("ABC_DEV")

http://msdn.microsoft.com/en-us/library/ms164660(VS.80).aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top