简体   繁体   中英

Custom role provider cache troubleshooting

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM