【问题标题】:RoleManager Cookie Expiring ImmediatelyRoleManager Cookie 即将过期
【发布时间】:2014-04-24 12:51:41
【问题描述】:

这曾经有效,但是我最近发现 ASP.NET 不再在 cookie 中缓存用户角色。我运行了一个提琴手跟踪,看起来 cookie 的值是空白的,并且过期日期设置在过去。因此,cookie 不会在后续请求中发送,并且每次往返都会命中 DB。

我似乎找不到任何关于此的帖子。任何帮助都会很棒。谢谢!

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>

提琴手响应(标头)

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

【问题讨论】:

    标签: asp.net asp.net-mvc


    【解决方案1】:

    看看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;
        }
    }
    

    【讨论】:

      【解决方案2】:

      我想。该会话没有任何角色。

      【讨论】:

      • 用户已分配角色。我可以在 RolesProvider 中设置断点并查看返回的角色数组。这与 Session 无关,因为 cookie 被用于缓存角色......而不是 Session。
      【解决方案3】:

      试试createPersistentCookie="true"

      【讨论】:

        猜你喜欢
        • 2012-06-03
        • 1970-01-01
        • 1970-01-01
        • 2012-04-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-13
        相关资源
        最近更新 更多