会话超时问题? [英] session timeout problem?

查看:68
本文介绍了会话超时问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

if (Session["username"].ToString() == null)
              {
                  Response.Redirect("~/Login.aspx");
              }
              else
              {
                  objloginpl.username = Session["username"].ToString();
                  objloginpl.password = Session["password"].ToString();
              }  





这是我的页面加载代码...



this is my page load code...

protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            lblyear26.Text = DateTime.Now.ToString("yyyy");

            string strPreviousPage = "";
            if (Request.UrlReferrer != null)
            {
                strPreviousPage = Request.UrlReferrer.Segments[Request.UrlReferrer.Segments.Length - 1];
            }
            if (strPreviousPage == "")
            {
                Response.Redirect("~/Login.aspx");
            }
            else
            {
                if (Session["username"].ToString() == null)
                {
                    Response.Redirect("~/Login.aspx");
                }
                else
                {
                    objloginpl.username = Session["username"].ToString();
                    objloginpl.password = Session["password"].ToString();

                    DataTable dtlogincheck1 = new DataTable();
                    dtlogincheck1 = objloginbal.GetLogindetails(objloginpl);

                    if (dtlogincheck1.Rows[0]["admin1"].ToString() == "VM" && dtlogincheck1.Rows[0]["branch"].ToString() == "Head Office")
                    {
                        lblUserName.Text = Session["username"].ToString();
                        lblLastLogin.Text = Session["lastlogin"].ToString();
                    }
                    else if (dtlogincheck1.Rows[0]["admin1"].ToString() == "VM" && dtlogincheck1.Rows[0]["branch"].ToString() != "Head Office")
                    {
                        lblUserName.Text = Session["username"].ToString();
                        lblLastLogin.Text = Session["lastlogin"].ToString();
                    }
                    else
                    {
                        Response.Redirect("~/Login.aspx");
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Log(ex.Message, ex.StackTrace);
        }
    }





最后,这一行得到......例外......

lblUserName.Text = Session [username]。ToString();

like

对象引用未设置为对象的实例。

这里会话[用户名]。当会话过期时ToString()没有获得任何名称...如何解决这个问题...请帮助我..



finally , this line gets..an exception..
lblUserName.Text = Session["username"].ToString();
like
Object reference not set to an instance of an object.
here Session["username"].ToString() is not getting any "name" when session will expired...how to solve this..please help me..

推荐答案

默认情况下,我们的网站会话超时是在该会话到期后20分钟,假设我们想在我们的应用程序中设置自定义超时,我们可以用不同的方式设置它(web.config,global.asax和in IIS)



检查以下方法在web.config,global.asax和iis中设置会话超时



在Web.config文件中,我们可以设置会话超时,如下所示



By default our websites session timeout is 20 mins after that session will gets expire suppose if we want to set our custom timeout in our applications we can set it in different ways (web.config, global.asax and in IIS)

Check below methods to set session timeout in web.config, global.asax and in iis

In Web.config file we can set session timeout like as shown below

<configuration>
<system.web>
 <sessionstate mode="InProc" timeout="60">
 </sessionstate>
 </system.web>
</configuration>





参考



Asp.net设置会话超时.. :) [ ^ ]





更新





如果会话已经过期,那么你就无法将它转换成.toString()的字符串

试试这个..



reference

Asp.net set session timeout.. :)[^]


Updated


if session is already expired then you can not convert it into string from .toString()
try this..

if(Session["username"]!=null || Session["username"]!="")
{
    lblUserName.Text = Convert.ToString(Session["username"]);

}
else

{
// cODE TO FILL SESSION VALUE
}


u可以把try ctach放在这里。



u can put try ctach here.

try
{
objloginpl.username = Session["username"].ToString();
objloginpl.password = Session["password"].ToString();
}
catch(Exception)
{
Response.Redirect("~/Login.aspx");
}


参考



会话超时问题 [ ^ ]


这篇关于会话超时问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆