代码之家  ›  专栏  ›  技术社区  ›  Brandon Montgomery

ASP.NET自定义角色提供程序不尊重cacheRolesInCookie=“true”

  •  6
  • Brandon Montgomery  · 技术社区  · 14 年前

    <roleManager enabled="true" defaultProvider="TDRoleProvider" cacheRolesInCookie="true">
      <providers>
        <clear/>
        <add name="TDRoleProvider" type="TDRoleProvider"/>
      </providers>
    </roleManager>
    

    我已经重写了自定义角色提供程序中的GetRolesForUser函数,并且已经进入其中,它可以正常工作-为我正在测试的用户加载60个角色。但是,我注意到,每次调用User.IsInRole的请求都会调用GetRolesForUser。在我编写的其他应用程序中,它只调用一次,然后将结果缓存到cookie中。由于某些原因,缓存对此应用不起作用。你知道为什么吗?

    3 回复  |  直到 7 年前
        1
  •  3
  •   holly_cheng    5 年前

    https://web.archive.org/web/20101123220352/http://connect.microsoft.com/VisualStudio/feedback/details/104688/rolemanager-cacherolesincookie-option-does-not-work

    “何时在RolePrincipal中缓存(或不缓存)的问题经历了多次设计迭代,最后我们决定只缓存由IPrincipal接口(即IsInRole)公开的方法。”

        2
  •  3
  •   Community CDub    7 年前

    this.Context.User = new GenericPrincipal(customIdentity, roles);
    

    这对我来说是固定的:

                HttpCookie roleCookie = this.Context.Request.Cookies[Roles.CookieName];
                if (IsValidAuthCookie(roleCookie))
                {
                    this.Context.User = new RolePrincipal(customIdentity, roleCookie.Value);
                }
                else
                {
                    this.Context.User = new RolePrincipal(customIdentity);
                    var x = this.Context.User.IsInRole("Visitor"); // do this to cache the results in the cookie
                }
    

    这个 方法检查null和空:

        private static bool IsValidAuthCookie(HttpCookie authCookie)
        {
            return authCookie != null && !String.IsNullOrEmpty(authCookie.Value);
        }
    

            HttpCookie roleCookie = filterContext.HttpContext.Request.Cookies[Roles.CookieName];
            if (IsValidAuthCookie(roleCookie))
            {
                filterContext.Principal = new RolePrincipal(customIdentity, roleCookie.Value);
                RolePrincipal rp = (RolePrincipal)filterContext.Principal;
                if (!rp.IsRoleListCached) // check if roles loaded properly (if loads old cookie from another user for example, roles won't be loaded/cached).
                {
                    // roles not loaded. Delete and save new
                    Roles.DeleteCookie();
                    rp.IsInRole("Visitor"); // load Roles
                    SaveRoleCookie(rp, filterContext);
                }
    
            }
            else
            {
                filterContext.Principal = new RolePrincipal(customIdentity);
                filterContext.Principal.IsInRole("Visitor"); // do this to cache the results in the cookie.
                SaveRoleCookie(filterContext.Principal as RolePrincipal, filterContext);
            }
    

    保存roleCookie

        private void SaveRoleCookie(RolePrincipal rp, AuthenticationContext filterContext)
        {
            string s = rp.ToEncryptedTicket();
            const int MAX_COOKIE_LENGTH = 4096;
            if (string.IsNullOrEmpty(s) || s.Length > MAX_COOKIE_LENGTH)
            {
                Roles.DeleteCookie();
            }
            else
            {
                HttpCookie cookie = new HttpCookie(Roles.CookieName, s);
                cookie.HttpOnly = true;
                cookie.Path = Roles.CookiePath;
                cookie.Domain = Roles.Domain;
                if (Roles.CreatePersistentCookie)
                    cookie.Expires = rp.ExpireDate;
                cookie.Secure = Roles.CookieRequireSSL;
                filterContext.HttpContext.Response.Cookies.Add(cookie);
            }
        }
    

    here .

        3
  •  1
  •   kheya    14 年前

    我也是这样。它一直在调用GetRolesForUser()