HN_TSP 2023-11-30 15:15 采纳率: 50%
浏览 17

VS2019+C#+.NETFramework4.5环境下session失效跳转登录界面失败

1、环境:VS2019+C#+.NETFramework4.5

2、问题:在登陆界面把用户的信息赋值给Session:
Session["userId"] = dr["UserId"].ToString();

Session["username"] = dr["UserName"].ToString();

3、在框架网页里的Page_Load里用trycatchfinally对Session["userId"] 的值进行判断,如果失效为空,关闭框架网页跳转登录界面:

    try
    {
        if (Session["userId"].ToString() == null)
        {
        }
    }
    catch
    {
        Response.Write("<script>alert('由于您长时间未进行操作,系统已为您自动退出登录!!!')</script>");
        Response.Write("<script language=javascript>window.parent.opener=null;window.parent.open('','_self','');window.parent.close();window.open('../default.aspx');</script>");
    }
    user_id.Text = Session["userID"].ToString();

  运行后当Session["userId"] 为空值时,逐条语句调试时,系统运行了二个Response.Write语句,但没出现提示和关闭框架网页跳转登录界面,停止在user_id.Text = Session["userID"].ToString();并提示:

System.NullReferenceException:“未将对象引用设置到对象的实例。”

请教:1、为什么运行了二个Response.Write语句,但没出现提示和关闭框架网页跳转登录界面;

2、这代码在VS2013+C#+.NETFramework2.0环境下可以正常运行,出现提示后关闭框架网页跳转登录界面。为什么在VS2019+C#+.NETFramework4.5环境下就出现问题,要如何修改才能正常?

  • 写回答

3条回答 默认 最新

  • 丨Haruna 2023-12-06 09:30
    关注
    1. Session["userId"] == null来检查 Session 是否存在并且为 null。
    2. 在不同版本之间的行为或默认设置可能会有所不同。在旧版本中可能有不同的默认行为或错误处理方式。
    try
    {
        if (Session["userId"] == null)
        {
            Response.Write("<script>alert('由于您长时间未进行操作,系统已为您自动退出登录!!!')</script>");
            Response.Write("<script language=javascript>window.parent.opener=null;window.parent.open('','_self','');window.parent.close();window.open('../default.aspx');</script>");
        }
        else
        {
            user_id.Text = Session["userId"].ToString();
        }
    }
    catch (Exception ex)
    {
        // 处理异常
        Response.Write("<script>alert('发生错误:" + ex.Message + "')</script>");
    }
    
    评论

报告相同问题?

问题事件

  • 创建了问题 11月30日

悬赏问题

  • ¥20 ARKts悬浮窗和快捷方式问题
  • ¥15 comparecluster没有办法kegg
  • ¥15 远程访问linux主机超时
  • ¥15 odoo17存货管理优势于中国国内该行业传统ERP或MES的详细解读和举例
  • ¥15 CPU卡指令整合指令数据都在图片上
  • ¥15 odoo17处理受托加工产品
  • ¥15 如何用MATLAB编码图三的积分
  • ¥15 圆孔衍射光强随孔径变化
  • ¥15 MacBook pro m3max上用vscode运行c语言没有反应
  • ¥15 ESP-PROG配置错误,ALL ONES