0

我编写了自定义角色提供程序,它在内部使用 Web 服务方法来获取角色或用户名。此提供程序继承自System.Web.Security.RoleProvider。在web.config文件中,我打开了 .NET 提供的使用 cookie 的缓存功能。

这是此提供程序的web.config部分的外观:

<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>

现在,它来测试缓存是否工作。我编写了如下所示的简单方法:

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

4

1 回答 1

1

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

于 2010-09-27T12:28:00.953 回答