4

I am trying to use forms authentication with the following configuration settings. I have set cacheRolesInCookie to true. However, I am finding that the RoleProvider's GetRolesForUser method is called on every request. I can see that the cookie .asproles is created and has data in it but it appears to be ignored.

Has anyone come across this problem before? Any help would be much appreciated.

    <authentication mode="Forms">
        <forms
            name=".formsauth"
            loginUrl="~/Login.aspx"
            defaultUrl="~/Home.aspx"
            slidingExpiration="true"
            timeout="20"
            path="/" 
             />
    </authentication>

    <!-- Membership Provider -->
    <membership defaultProvider="CustomMembersipProvider">
        <providers>
            <add name="CustomMembersipProvider"
                 type="Company.Membership.CustomMembersipProvider" />
        </providers>
    </membership>

    <!-- Role Provider -->
    <roleManager defaultProvider="CustomMembershipRoleProvider"
            enabled="true"
            cacheRolesInCookie="true"
            cookieName=".asproles"
            cookieTimeout="20"
            cookieSlidingExpiration="true"
            cookieProtection="All" createPersistentCookie="true">
        <providers>
            <add name="CustomMembershipRoleProvider"
                 type="Company.Membership.Provider.CustomMembershipRoleProvider" />
        </providers>
    </roleManager>

Many thanks Naren

Naren
  • 704
  • 1
  • 7
  • 15

2 Answers2

1

What method of RolePrincipal is called? IsInRole method uses cache in .asproles cookie but GetRoles method triggers call to your RoleProvider once per request.

Andrej Golcov
  • 648
  • 5
  • 12
0

After I upgraded my app to MVC5 .Net 4.5 started having the same issue. To fix you will have to save the cookie yourself. See how here.

Community
  • 1
  • 1
Joao Leme
  • 9,598
  • 3
  • 33
  • 47