当OnAuthorization方法被调用? [英] When OnAuthorization method is called?

查看:295
本文介绍了当OnAuthorization方法被调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现自定义的方法:

I have implemented custom method for making user information available to Views with smth like that:

    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改变它并没有反映在视图。难道我只是在每次调用这里有更聪明的办法,即另一种方法,这有助于自动更新用户信息更新呢?

The problem is when user information (especially permissions) are changed in DB it is not reflected in Views. Shall I just update it on every call here on there is smarter way, i.e. other method which helps to update user information automatically?

推荐答案

OnAuth方法被调用。

OnAuth method is called when a process requests authorization.

您会发现大量的材料在网上的,在这里加入他们几个

You will find lot of material online, adding few of them here

http://blog.tomasjansson.com/securing-您-ASP净MVC-3的应用程序
http://schotime.net/blog/index.php/2009/02/17/custom-authorization-with-aspnet-mvc/

<一个href=\"http://patrickdesjardins.com/blog/make-all-actions-of-controller-securized-with-authorise-filter-implicit\" rel=\"nofollow\">http://patrickdesjardins.com/blog/make-all-actions-of-controller-securized-with-authorise-filter-implicit

这篇关于当OnAuthorization方法被调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆