何时调用OnAuthorization方法?

时间:2013-08-07 13:17:03

标签: c# asp.net-mvc

我已经实现了自定义方法,可以像这样使用smth为视图提供用户信息:

    protected override void OnAuthorization(AuthorizationContext filterContext)
    {
        if (HttpContext.User != null)
        {
            HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
            if (authCookie != null)
            {
                FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                AWESOMEUser user = serializer.Deserialize<AWESOMEUser>(authTicket.UserData);
                if (user == null)
                {
                    HttpContext.User = null;
                }
                else
                {
                    HttpContext.User = new PlatformUser(typeof(DBMembershipProvider).Name, user);
                }
            }
            else
            {
                HttpContext.User = null;
            }

        }
        base.OnAuthorization(filterContext);
}

问题是当在DB中更改用户信息(尤其是权限)时,它不会反映在视图中。我应该在每次调用时更新它,这里有更聪明的方法,即有助于自动更新用户信息的其他方法吗?