asp.net-mvc - ASP.NET MVC Filter AuthorizeAttribute 怎么设置例外?
本文介绍了asp.net-mvc - ASP.NET MVC Filter AuthorizeAttribute 怎么设置例外?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
[AttributeUsage(AttributeTargets.All, AllowMultiple = true, Inherited = true)]
public class LoginFilter : AuthorizeAttribute {……}
这样写的话所有的页面包括login页面都会被过滤掉,我希望login页面除外,应该在哪里设置?
在web.config里里写了也没有起作用。
解决方案
public override void OnAuthorization(AuthorizationContext filterContext)
{
string path = filterContext.HttpContext.Request.CurrentExecutionFilePath;
if (path.StartsWith("/Home/Index",StringComparison.CurrentCultureIgnoreCase)) { return; }
HttpCookie cookieName = System.Web.HttpContext.Current.Request.Cookies.Get("name");
//判断Cookie用户名密码是否存在
if (cookieName == null)
{
filterContext.Result = new RedirectResult("/Home/Index");
}
}
[AllowAnonymous]
是无效的。
这篇关于asp.net-mvc - ASP.NET MVC Filter AuthorizeAttribute 怎么设置例外?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文