您的位置:首页 > 其它

mvc 验证登录

2016-01-27 09:25 302 查看
很多时候,我们需要多个页面验证用户是否登录

有2中方法。

一种是继承 Attrbuite属性,添加验证,这个可以网上搜索。

我一般使用下面的方式

创建BaseWebController继承Controller。

然后实现OnActionExcuting方法,这样所有继承BaseWebController的Controller中,访问Action时,都会先跑到这里,如果没有登录,就会跳转到Login页面

public class BaseWebController : Controller
{

protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
//user为空,并且不是登录页面,则跳转到登录页面。
if ((filterContext.HttpContext.Session["User"] == null || CurrentUser.id == 0)
&& (controllerName != "Login" && actionName != "Login"))
{
filterContext.HttpContext.Response.Redirect("/Login/Login");
}

base.OnActionExecuting(filterContext);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: