Session expires before the values set in web.config

1

How about Devs,     I am developing a Web forms application with asp.net, I can not make the session expire before 20 minutes, I have a login screen where when I authenticate I keep the authenticated user in a session variable, this I do so keep the session within the system (it is not the most correct way), the issue is that after 20 minutes of inactivity I returned to the login, the other is that for some reason if I am working within the system after 20 minutes I also take out of the system, how can I solve this, attached source code of the settings of how I manage the session:

web.config:     

      

login screen:

protected void btnLogin_Click (object sender, EventArgs e)         {

        string sUsuario = tbUserName.Text;
        string sPassword = tbPassword.Text;
        DataSet dsUsuario;


        if (!string.IsNullOrEmpty(sUsuario) && !string.IsNullOrEmpty(sPassword))
        {

            try
            {
                if (objControllerSeguridad.AutenticarUsuario(sUsuario, sPassword))
                {
                    dsUsuario = objControllerSeguridad.DsReturn;
                    if (dsUsuario.Tables["Usuario"].Rows.Count > 0)
                    {
                        Session["usuario"] = dsUsuario.Tables["Usuario"].Rows[0]["USUARIO"].ToString();
                        Session["nombreUsuario"] = dsUsuario.Tables["Usuario"].Rows[0]["NOMBRE"].ToString();
                        Session["idRol"] = dsUsuario.Tables["Usuario"].Rows[0]["ID_ROL"].ToString();
                        Session["rol"] = dsUsuario.Tables["Usuario"].Rows[0]["ROL"].ToString();

                        Response.Redirect("~/", false);


                    }
                    else
                    {
                        FailureText.Text = "Usuario o Clave incorrecta.";
                        ErrorMessage.Visible = true;
                        return;

                    }
                }
                else
                {
                    FailureText.Text = "Ocurrio un error, consulte con el administrador del sistema.";
                    ErrorMessage.Visible = true;
                    return;

                }

            }
            catch (Exception ex)
            {
                log.LogError(ex.ToString(), ex.StackTrace);

            }


        }

    }

And in the master page is where I control the user session variable that has not expired: protected void Page_Load (object sender, EventArgs e)         {             try             {                 var sCookie = Request.Cookies ["Cookie"]. ToString ();

            //if (Session["usuario"] == null || Session["idRol"] == null || String.IsNullOrEmpty(sCookie))
            if (Session["usuario"] == null || Session["idRol"] == null)
            {
                Response.Redirect("~/Account/Login.aspx");
            }else
                BuildMenu(dxMenu, SqlDtsBanco);
            if(dxMenu.SelectedItem != null)
                Session["id_menu"] = dxMenu.SelectedItem.ToString();
        }
        catch (Exception ex)
        {
            Response.Redirect("~/Account/Login.aspx");
            //Session.Abandon();
        }

    }

}

    
asked by Jose Felix 29.09.2017 в 23:28
source

1 answer

2

You could set the expiration time of Session of the site

Session Status Modes

the idea is that you change in web.config the time

<configuration>
  <system.web>
    <sessionState mode="StateServer"
      stateConnectionString="tcpip=SampleStateServer:42424"
      cookieless="false"
      timeout="20"/>
  </system.web>
</configuration>

you could increase it for the time, but remember that the inactivity time is taken when no invocation is made to the server, you can be working in the browser but if a request never travels, the session will expire.

You could apply but this technique

[ASP.NET] Keeping the Session Active Indefinitely

there I explain how by means of ajax you can invoke the server to prevent it from expiring

    
answered by 30.09.2017 в 00:07