【问题标题】:Prompt user and redirect if session is expired如果会话过期,提示用户并重定向
【发布时间】:2014-08-01 18:04:39
【问题描述】:

我在一个页面上有一个按钮,可以打开一个带有汇率计算器的窗口。 汇率计算器是一项外部网络服务。 可能会发生会话已过期但用户仍在页面上并单击打开此类窗口的按钮,而不是计算器,而是显示网站登录页面。 为了避免用户从窗口中的页面登录,我试图检查会话是否仍然存在,如果没有提示用户会话已过期,他需要再次登录。 为了避免尽管警告用户仍然尝试从窗口登录,我想在用户单击提示按钮中的确定时停止窗口中的页面加载并重定向到登录页面。

public void Page_Init(object o, EventArgs e)
{
    if (Session ["isUser"] != "isUser")
    {

        ScriptManager.RegisterStartupScript(
        this,
        this.GetType(),
        "Warning",
        "alert('Your session expired! You shall login again.');",
        true);
        Response.Redirect("~/Login.aspx");
    }
}

上述方法有时会显示提示,有时却没有,无论如何更新到最后一行重定向到登录页面。我需要一些帮助来控制上述情况。我怎样才能使上述逻辑工作?我的意思是如果条件不满足,页面没有加载并重定向用户是否可以显示提示?

【问题讨论】:

  • 如果您不使用弹出窗口,这个问题会变得更容易管理。这是必要的设计“功能”吗?
  • @Yuck,并不是真的只是“很高兴拥有”,但我可以把它拿走。但是,即使我只留下最后一行代码(重定向),它也会重定向窗口而不是页面。
  • @Yuck,我已经改变了我上面的评论。

标签: c# asp.net session


【解决方案1】:

我刚刚在我的项目中实现了这个,它现在可以正常工作了:

1) 实现一个处理会话的类

 public static class SessionManager
    {


        public static object _Session_UserInfo
        {

            get
            {

                if (HttpContext.Current.Session["isUser"] == null)
                {
                    HttpContext.Current.Response.Redirect("Index?Expired=1");
                    return null;
                }

                else
                {
                    return HttpContext.Current.Session["isUser"];
                }
            }


            set
            {
                HttpContext.Current.Session["isUser"] = value;


            }

        }



    }

//2) 从这个 calss 开始引用 sesison 变量:

public void Page_Init(object o, EventArgs e)
{
     //do whatever you wanna do with it, it will automatically redirect if expired
     //SessionManager._Session_UserInfo;


}

//在javascript中执行以下操作:

function getParameterByName(n) {
            var half = location.search.split(n + '=')[1];
            return half !== undefined ? decodeURIComponent(half.split('&')[0]) : null;
        }

        $(document).ready(function () {

            var _IsSessionExpired = getParameterByName("Expired");
          //  alert(_IsSessionExpired);
            if (_IsSessionExpired != null)
                alert('Your Session Has Expired, please login to the system !');
                      window.location="any page you want to redirect to";

        });

欢迎您

【讨论】:

  • 感谢 Wizpert,我认为我的主要问题不是检查会话,而是关闭窗口并重定向。我面临的问题是,如果我关闭窗口,我将无法重定向。所以我正在考虑在打开窗口之前调用会话检查并从 javascript 函数重定向。不过,感谢您的支持+。
【解决方案2】:

我终于找到了解决问题的方法。为了避免试图从我需要关闭的窗口重定向的永无止境的循环,我从用于打开窗口的 javascript 函数中完成了它。 所以基本上在我打开窗口之前,我会检查会话是否有效,如果不是,我会在打开之前重定向。

function openRadWin() {

   var session='<%= Session["isUser"]%>';
   if (session != "isUser") {
   alert("Your Session has expired. Click OK to login again.");
   window.location = "../Login.aspx";
   }

   else {
   var width = "430px";
   var height = "355px";
   var left = "800px";
   var top = "150px";
   radopen("currencies.aspx", "RadWindow1", width, height, left, top);
    }
   }

感谢 Wizpert 的一些好建议,但我决定尽量减少代码并用几行代码完成所有工作。

【讨论】:

    【解决方案3】:

    您可以尝试此代码,它使用 WebMethod 使当前登录会话 cookie 无效,然后提醒用户会话到期,然后将用户重定向到登录页面。

        <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
    
        <!DOCTYPE html>
    
        <html xmlns="http://www.w3.org/1999/xhtml">
        <head runat="server">
        <title></title>
        <script type="text/javascript">
        var HeartBeatTimer;
    
    
        function StartHeartBeat()
        {
            if (HeartBeatTimer == null) {
                //timouts after 1 minute
                HeartBeatTimer = setInterval("HeartBeat()", 1000 * 60*1);
            }
        }
    
        function HeartBeat()
        {
            PageMethods.PokePage(onSucess, onError);
            function onSucess(result) {
                alert('Session Expired');
                window.location = result;
            }
            function onError(result) {
                alert('Something wrong.');
            }
        }
        </script>
        </head>
        <body   onload="StartHeartBeat();">
        <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"></asp:ScriptManager>
        <div>
    
        </div>
        </form>
        </body>
        </html>
    

    还有 C# 代码:

        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Web;
        using System.Web.Services;
        using System.Web.UI;
        using System.Web.UI.WebControls;
    
        namespace WebApplication1
        {
        public partial class WebForm1 : System.Web.UI.Page
        {
        static int x = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            HttpCookie hc = new HttpCookie("kk", "kk");
            hc.Expires = DateTime.Now.AddMinutes(5);
            Response.Cookies.Add(hc);
        }
        [WebMethod(EnableSession = true)]
        public static String PokePage()
        {
            HttpCookie hc = new HttpCookie("kk", "kk");
            hc.Expires = DateTime.Now.AddMinutes(-5);
            HttpContext.Current.Response.Cookies.Add(hc);
            return "http://www.google.com";
        }
        }
        }
    

    【讨论】:

    • 非常感谢您的建议。我改变了路线,并按照我自己在此处发布的答案解决了问题。但是,感谢您的支持。谢谢
    猜你喜欢
    • 1970-01-01
    • 2015-08-17
    • 2017-02-07
    • 1970-01-01
    • 1970-01-01
    • 2016-09-02
    • 2011-11-28
    • 1970-01-01
    • 2019-02-08
    相关资源
    最近更新 更多