功能實現:ajax
登陸時添加session緩存.判斷是否登陸過時.json
1.判斷是否須要登陸判斷緩存
public static AdminLoginUser GetAdminLoginUser()
{
#region 獲取當前登陸者信息
AdminLoginUser result = null;
try
{
if (HttpContext.Current.Session["User"] != null)
{
result = HttpContext.Current.Session["User"] as AdminLoginUser;
}
else
{
result = null;
}
}
catch (Exception ex)
{
//TTracer.WriteLog(ex.ToString());
}
return result;
#endregion
}session
public class SessionAndAuthority : ActionFilterAttribute//ActionFilterAttribute是Action過濾類,該屬於會在執行一個action以前先執行. { //後臺登陸用戶 protected AdminLoginUser adminloginUser { get { return Test1.Common.UserHelper.GetAdminLoginUser(); } } /// <summary> /// 使用驗證時 [NoSign] 標註不須要登陸和權限驗證 /// </summary> [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true)] public class NoSignAttribute : Attribute { } //操做是否須要判斷 private static bool SkipNoSign(ActionExecutingContext actionContext) { return actionContext.ActionDescriptor.GetCustomAttributes(typeof(NoSignAttribute), true).Length == 1;//有NoSign屬性 true } //在執行操做方法以前 判斷登陸狀況和頁面權限 public override void OnActionExecuting(ActionExecutingContext filterContext) { if (SkipNoSign(filterContext))//是否該類標記爲NoSign,若是是則不須要判斷 { base.OnActionExecuting(filterContext); return; } #region 先判斷session if (null == adminloginUser) { //session 過時 if (!filterContext.HttpContext.Request.IsAjaxRequest()) { // 請求跳轉到Tip頁面 filterContext.Result = new RedirectResult("/Home/Tip?state=0"); } else { //ajax請求 返回json格式提示 if (filterContext.HttpContext.Request.HttpMethod == "GET") { filterContext.Result = new RedirectResult("/Home/Tip?state=0"); } else { ContentResult content = new ContentResult(); ResultMessage msg = new ResultMessage() { success = false, message = "登陸已過時,請從新登陸!" }; content.Content = msg.ToJson(); filterContext.Result = content; } } } #endregion } }
2.登陸時添加緩存ide
HttpContext.Session["User"] = LoginUserInfo;
3.在 FilterConfig 添加過濾器 SessionAndAuthority spa