首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

受权过滤器

2013-09-13 
授权过滤器我自定了一个授权过滤器,验证如果没有通过就返回到登录页面/Account/Login问题:有些操作ajax,如

授权过滤器

我自定了一个授权过滤器,验证如果没有通过就返回到登录页面/Account/Login

问题:有些操作ajax,如果没有登录,希望返回的是一个代码,比如一个整数。  该怎么做?  


webconfig
    <authentication mode="Forms">
      <forms loginUrl="~/Account/Login" timeout="2880" />
    </authentication>



public class UserAuthorize : AuthorizeAttribute
    {
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            if (!httpContext.Request.IsAuthenticated)                               //首要条件:必须登录,必须有身份验证票。
                return false;   

            string controller = httpContext.Request.RequestContext.RouteData.Values["controller"].ToString();
            string action = httpContext.Request.RequestContext.RouteData.Values["action"].ToString();

            using (var db = new WEBVODEntities())
            {
                List<CtrlActRole> tempList = db.CtrlActRole.Where(a => a.ControllerName == controller && a.ActionName == action).ToList();
                if (tempList.Count == 0)                                            //如果没有找到记录,表示不需要权限控制
                    return true;

                foreach (var ctrlActRole in tempList)
                {

                    string roleField = ctrlActRole.RoleField;                      //获取对应的数据库字段



                    RegisteredUser oldUser = CommonUtils.GetCurrentUser();
                    if (oldUser == null)
                        return false;

                    var roleList = db.ExecuteStoreQuery<Role>("select top 1 * from role where " + roleField + "=1 and id={0}", oldUser.RoleId).ToList();  // 查看相应的角色,对应字段是否==true

                    if (roleList.Count > 0)                                             //如果有记录,表示有权限(这个地方应该显示相应的提示,不是直接返回到登录界面)
                        return true;
                }
  
            }
            return false;
        }
    }


[解决办法]
你找的代码是对的。

要返回数字,这里
                   new JsonResult
                {
                    Data = 整数值,
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet,


                     
                };

热点排行