net.AspMVC中超时弹出窗口跳转功能的实现

来源:爱站网时间:2020-08-17编辑:网友分享
在网站开发过程中,需要有一个功能,即用户登录网站后,如果在一段时间内(如20分钟)没有操作,应该自动退出网站,下面爱站技术频道小编给大家介绍net.AspMVC中超时弹出窗口跳转功能的实现,希望对你有所帮助。

在网站开发过程中,需要有一个功能,即用户登录网站后,如果在一段时间内(如20分钟)没有操作,应该自动退出网站,下面爱站技术频道小编给大家介绍net.AspMVC中超时弹出窗口跳转功能的实现,希望对你有所帮助。

 public class PowerFilter : AuthorizeAttribute
  {
    public override void OnAuthorization(AuthorizationContext filterContext)
    {
      var cookie = HttpContext.Current.Request.Cookies["loginInfo"];
      if(null == cookie)
      {
        filterContext.Result = new RedirectResult("/admin/login/index");
      }
      else
      {
        cookie.Expires = DateTime.Now.AddMinutes(30);
        HttpContext.Current.Response.Cookies.Remove("loginInfo");
        HttpContext.Current.Response.Cookies.Add(cookie);
      }
    }
  }

但是页面直接跳转了,也没有一个提示,显得不是很友好,可以这样

public class PowerFilter : AuthorizeAttribute
  {
    public override void OnAuthorization(AuthorizationContext filterContext)
    {
      var cookie = HttpContext.Current.Request.Cookies["loginInfo"];
      if(null == cookie)
      {
        filterContext.Result = new ContentResult()
        {
          Content = string
          .Format("","/admin/login/index")
        };
      }
      else
      {
        cookie.Expires = DateTime.Now.AddMinutes(30);
        HttpContext.Current.Response.Cookies.Remove("loginInfo");
        HttpContext.Current.Response.Cookies.Add(cookie);
      }
    }
  }
}

但是,假如是ajax请求呢?

public class PowerFilter : AuthorizeAttribute
  {
    public override void OnAuthorization(AuthorizationContext filterContext)
    {
      var cookie = HttpContext.Current.Request.Cookies["loginInfo"];
      if(null == cookie)
      {
        if(!filterContext.HttpContext.Request.IsAjaxRequest())
        {
          filterContext.Result = new ContentResult()
          {
            Content = string
                 .Format("","/admin/login/index")
          };
        }
        else
        {
          filterContext.Result = new JsonResult()
          {
            Data = new { logoff = true,logurl = "/admin/login/index" },
            ContentType = null,
            ContentEncoding = null,
            JsonRequestBehavior = JsonRequestBehavior.AllowGet
          };
        }
      }
      else
      {
        cookie.Expires = DateTime.Now.AddMinutes(30);
        HttpContext.Current.Response.Cookies.Remove("loginInfo");
        HttpContext.Current.Response.Cookies.Add(cookie);
      }
    }
  }

以上就是爱站技术频道小编给大家介绍的net.AspMVC中超时弹出窗口跳转功能的实现,作为程序员的入门知识,希望大家都能掌握的牢靠一点。

上一篇:Webapi实现通信加密的方法

下一篇:信号机发送页面跳转通知的方法

您可能感兴趣的文章

相关阅读

热门软件源码

最新软件源码下载