会话超时和页面重定向问题 [英] Session Timeout and Page Redirecting Problem

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

问题描述



在我开发的应用程序中,我想在用户登录时启动会话,并在用户注销或会话超时时结束.

我在web.config文件中添加了以下代码,

Hi,

In the application I develop, I want to start session when user log in and to end when user log out or session timeouts.

I added the following code in web.config file,

<system.web> 
<sessionstate mode="InProc" cookieless="false" timeout="1"></sessionstate> 
<authentication mode="Forms"> 
<forms timeout="1"></forms> 
</authentication> 
</system.web> 



我在login.aspx上控制了用户登录;



I controlled the user login at login.aspx;

SqlConnection myConnect = new SqlConnection(); 
myConnect.ConnectionString = "Data Source=localhost; database=myWebSite; Integrated Security=true"; 
myConnect.Open(); 
SqlDataReader rd; 
String sqlString = "Select * From user where user_name= @userName and user_password= @userPassword"; 

SqlCommand myCommand = new SqlCommand(sqlString, myConnect); 
SqlParameter p1 = new SqlParameter("@userName", txt_userName.Text); 
SqlParameter p2 = new SqlParameter("@userPassword", txt_userPassword.Text); 

myCommand.Parameters.Add(p1); 
myCommand.Parameters.Add(p2); 
rd= myCommand.ExecuteReader(); 
if (rd.Read()) 
{ 
lbl_alert.Text ="Welcome" + rd["user_name"].ToString();
lbl_alert.Visible = true; 
Session["userID"] = rd["userId"].ToString(); 
if (Session["logged"] != null) 
{ 
Session["logged"] = 1; 
} 
} 
else 
{ 
lbl_alert.Text = "Wrong User name or Password!.."; 
lbl_alert.Visible = true; 
} 
rd.Dispose(); 
myConnect.Close(); 



在global.asax,我在函数中添加了以下代码



At global.asax, I added the following code in functions

void Session_Start(object sender, EventArgs e)
{
       if ((Convert.ToInt32(Session[logged])==1)
          Response.Redirect("Home.aspx");
}

void Session_End(object sender, EventArgs e)
{
    if ((Convert.ToInt32(Session["logged"])!=1)
       Response.Redirect("Login.aspx");
}



当用户登录时,它不会按照我在代码中设置的那样将用户定向到主页.刷新页面一分钟后,同样,它也不会将用户重定向到登录页面.此外,我更改了在IIS端将超时属性设置为1分钟.

我该怎么办?

在此先感谢您的答复..



When user logged in ,it doesn''t direct the user to home page as I set in the code.And after 1 minute when I refresh the page ,again it doesn''t redirect the user to login page.Also, I changed the timeout property to 1 minute at IIS side.

What should I do?

Thanks in advance for your replies..

推荐答案

请查看ASP.NET页的生命周期.在处理登录页面之前,会话已经开始.看起来合乎逻辑吗?在没有启动会话的情况下如何访问Session变量?

成功登录后,请重定向用户.

另外,应该更早进行此检查.如果用户已经登录,为什么要进行数据库调用?

Please view the ASP.NET page life cycle. The session has been started before the login page is processed. Seems logical right? How could you access a Session variable without the session having been started already?

Redirect the user after they have successfully logged in.

Also, this check should be made much earlier. Why go through the database call if the user is already logged in?

if(Session["logged"] != null)



最重要的是,使用内置的ASP.NET登录控件和功能会更好.会话状态和重定向将按照web.config文件中的配置自动进行处理.



Most importantly, you would be much better off using the built-in ASP.NET login controls and functionality. The session state and redirects would be handled automatically as configured in the web.config file.


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

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