日期:2014-05-17  浏览次数:20440 次

授权过滤器

我自定了一个授权过滤器,验证如果没有通过就返回到登录页面/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;                      //获取对应的数据库字段