2012-12-31 57 views
1

這用於工作,但是我最近發現ASP.NET不會在cookie中緩存用戶角色。我運行了一條提琴手曲線,看起來cookie的值是空的,到期日期是過去設置的。因此,cookie不會在後續請求中發送,並且每次往返都會觸發數據庫。RoleManager Cookie立即過期

我似乎無法找到關於此的任何帖子。任何幫助都會很棒。謝謝!

的web.config

<roleManager enabled="true" defaultProvider="MyRoleProvider" cacheRolesInCookie="true" cookieName=".ASPXROLES" cookieTimeout="30" cookiePath="/" cookieRequireSSL="false" cookieSlidingExpiration="true" cookieProtection="All" createPersistentCookie="false"> 
    <providers> 
    <clear /> 
    <add name="MyRoleProvider" type="MyCompany.Core.Web.Providers.MyRoleProvider" connectionStringName="MainConnect" applicationName="MyApplication" /> 
    </providers> 
</roleManager> 

的Fiddler響應(報頭)

HTTP/1.1 200 OK 
Cache-Control: private, s-maxage=0 
Content-Type: text/html; charset=utf-8 
Content-Encoding: gzip 
Vary: Accept-Encoding 
Server: Microsoft-IIS/8.0 
X-AspNetMvc-Version: 4.0 
X-AspNet-Version: 4.0.30319 
Set-Cookie: .ASPXROLES=; expires=Tue, 12-Oct-1999 05:00:00 GMT; path=/; HttpOnly 
X-Powered-By: ASP.NET 
Date: Mon, 31 Dec 2012 01:14:19 GMT 
Content-Length: 1381 

回答

0

我想。該會議沒有任何作用。

+0

的用戶分配的角色。我可以在RolesProvider中設置斷點並查看返回的角色數組。這與Session無關,因爲Cookie正用於緩存角色......而不是Session。 –

0

嘗試createPersistentCookie="true"

0

看看This answer。它似乎表明只有提供者的IsUserInRole成員會以這種方式緩存結果。在檢查用戶角色時,ASP .NET MVC似乎專門使用GetRolesForUser。不久之前我碰到了這個相同的限制 - 這裏是我添加到角色提供者的一些代碼,以提供一個簡單的緩存機制。

public class MyRoleProvider : RoleProvider 
{ 
    private readonly string userRoleCacheKeyFormat; 

    public MyRoleProvider() 
    { 
     userRoleCacheKeyFormat = this.Name + "_{0}"; 
    } 

    public override string[] GetRolesForUser(string username) 
    { 
     return GetUserRoles(username); 
    } 

    private string[] GetUserRoles(string username) 
    { 
     string[] roleNames = null; 

     if (!TryGetCachedUserRoles(username, out roleNames)) 
     { 
      //cache miss 
      roleNames = GetUserRolesFromStore(username); 
     } 

     return roleNames; 
    } 

    private bool TryGetCachedUserRoles(string username, out string[] userRoles) 
    { 
     string cacheKey = string.Format(userRoleCacheKeyFormat, username); 
     HttpContext httpContext = HttpContext.Current; 
     if (httpContext != null) 
     { 
      userRoles = (string[])httpContext.Cache.Get(cacheKey); 
     } 
     else { userRoles = null; } 

     return (userRoles != null); 
    } 

    private void CacheUserRoles(string username, string[] userRoles) 
    { 
     string cacheKey = string.Format(userRoleCacheKeyFormat, username); 
     HttpContext httpContext = HttpContext.Current; 
     if (httpContext != null) 
     { 
      httpContext.Cache.Insert(cacheKey, userRoles, null, DateTime.UtcNow.AddMinutes(15), Cache.NoSlidingExpiration); 
     } 
    } 

    private string[] GetUserRolesFromStore(string username) 
    { 
     MyDbContext db = MvcApplication.IoC.Resolve<MyDbContext>(); 

     string[] roleNames = db.Users 
      .Single(u => u.Username == username) 
      .UserRoles 
      .Select(r => r.Name) 
      .ToArray(); 

     CacheUserRoles(username, roleNames); 

     return roleNames; 
    } 
}