在MVC中,继承重写AuthorizeAttribute方法来校验用户是否登录,
public class TestMemberAuthorizeAttribute : AuthorizeAttribute { protected override bool AuthorizeCore(System.Web.HttpContextBase httpContext) { var isAuthorized = false; if (httpContext!=null&&httpContext.Session!=null) { if (HttpContext.Current.Session["UserName"] != null) { isAuthorized = true; } } return isAuthorized; } protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext) { string path = filterContext.HttpContext.Request.Path; const string strUrl = "/Home/Index?ReturnUrl={0}"; filterContext.HttpContext.Response.Redirect(string.Format(strUrl, HttpUtility.UrlEncode(path)), true); } }
然后在Controller的Action上加上[TestMemberAuthorize]的Attribute后,貌似Action里面的代码还是会执行的,然后才到TestMemberAuthorizeAttribute 的Response.Redirect操作,这样的正常的吗,或者怎么样能使TestMemberAuthorize执行结束后才执行Action中的代码吗
谢谢
var routeValue = new RouteValueDictionary { { "Controller", "Home"}, { "Action", "Index"}, { "ReturnUrl", path} }; filterContext.Result = new RedirectToRouteResult(routeValue);
不要用Response.Redirect,这样跳转
多谢多谢,很好的解决办法。搞定了。感谢