日期:2014-05-18  浏览次数:20555 次

mvc3全局筛选器问题,拒绝匿名用户访问!
想做一内部系统,限制所有未登录用户访问,看了半天,知道要用全局筛选器,就是不知道怎么用?那位能给个例子,谢谢先

------解决方案--------------------
<authorization>
<deny users="?" />
</authorization>
放在web.config里的<system.web>下
------解决方案--------------------
在global里面的
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new 自定义过滤器());
}
------解决方案--------------------
C# code

public class AuthorizeLogin : AuthorizeAttribute
    {
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            string url = httpContext.Request.RawUrl;
            if (url.Equals("/") || url.Contains("/Home/LoginOn/") || url.Contains("/Home/CheckCode"))
            {
                return true;
            }
            if (Admin.Commom.AdminInfo.Session == null)
            {
                httpContext.Response.StatusCode = 404;
                return false;
            }
            else
            {
                return true;
            }
        }

        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            base.OnAuthorization(filterContext);
            if (filterContext.HttpContext.Response.StatusCode == 404)
            {
                filterContext.Result = new RedirectResult("/");
            }
        }