Naren
Naren

Reputation: 724

cacheRolesInCookie not caching roles

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

Upvotes: 4

Views: 1362

Answers (2)

Joao Leme
Joao Leme

Reputation: 9898

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.

Upvotes: 0

Andrej Golcov
Andrej Golcov

Reputation: 648

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

Upvotes: 1

Related Questions