您的位置:首页 > 其它

在执行Action之间检验是否登录

2015-12-16 15:19 411 查看
在执行Action之间检验是否登录,也可以在执行Action前先执行某一个操作

public class BaseController : Controller
{
protected string hostUrl = "";
/// <summary>
/// Action执行前判断
/// </summary>
/// <param name="filterContext"></param>
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
// url
this.hostUrl = "http://" + this.Request.Url.Host;
this.hostUrl += this.Request.Url.Port.ToString() == "80" ? "" : ":" + this.Request.Url.Port;
this.hostUrl += this.Request.ApplicationPath;

if (!this.checkLogin())// 判断是否登录
{
filterContext.Result = RedirectToRoute("Default",new{ Controller = "Login", Action = "Index" });
}

base.OnActionExecuting(filterContext);

}

/// <summary>
/// 判断是否登录
/// </summary>
protected bool checkLogin()
{
if (this.Session["userinfo"] == null)
{
return false;
}
return true;
}

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