Click here to Skip to main content
15,887,268 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm using javascript in aspx page to log-out inactive user. But also i need to change user status "0" when logged-out automatically. How can i send the value user status "0" when inactive user will logged-out automatically?

What I have tried:

C#
public void ValidateUser_LOGSTS_Update(object sender, EventArgs e)
    {
        int userId = 0;

        string Cur_user = Session["Userid"].ToString();
        String constr = ConfigurationManager.ConnectionStrings["abcd"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand("sp_tbl_LogCheck_update"))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@userID", Cur_user);
                cmd.Parameters.Add("@msg", SqlDbType.Int, 20).Direction = ParameterDirection.Output;
                cmd.Connection = con;
                con.Open();
                cmd.ExecuteNonQuery();

                // read output value from @NewId
                string msg_sts = Convert.ToString(cmd.Parameters["@msg"].Value);

                con.Close();
                if (msg_sts == "0")
                {
                    Session.Clear();
                    Session.Abandon();
                    //Response.Redirect(FormsAuthentication.LoginUrl);
                    FormsAuthentication.RedirectToLoginPage();
                }


            }

        }

    }


Java
<pre><script type="text/javascript">

    var t;
    window.onload=resetTimer;
    document.onkeypress=resetTimer;

    function logout()
    {
	    alert("You are now logged out.")
	    location.href = 'LogOut.aspx'
    }
    function resetTimer()
    {
	    clearTimeout(t);
	    t=setTimeout(logout,60000) //logs out in 10 min
    }

    </script>
Posted
Updated 16-Oct-19 2:34am

1 solution

The simpler solution is not to. In stead of logging him out when he's inactive and the browser is open, store a "logout expires" timestamp on the server and update it every time the server talk to him. Then when you need to know if he is logged in, you check the timestamp and bingo!

This means that if you powers down his PC for example, or loses the WiFi signal on his phone, he can still be logged off automatically when the timeout expires. Doing it your way leaves him logged in a week later if he does power down or lose the connection.
 
Share this answer
 
Comments
Faisal_Raj 16-Oct-19 8:58am    
Please give me example code , so i can follow that .
OriginalGriff 16-Oct-19 9:06am    
What part of it is difficult for you?
Faisal_Raj 16-Oct-19 9:14am    
How to set timestamp , please give me some example code
OriginalGriff 16-Oct-19 9:26am    
You are kidding, right?

DateTime expiresAt = DateTime.UtcNow.AddMinutes(30);


Just put that in your DB - which your code shows you know how to do ...
Faisal_Raj 16-Oct-19 9:36am    
How can I send the value user status "0" ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900