locked
Session start runs or fires twice in ASP.NET when debug through Visual Studio for a single request. Why? RRS feed

  • Question

  • User1094269964 posted

    Hi Team..Refer the below SessionStart and sessionend code.

    void Session_Start(object sender, EventArgs e)
    {
    string sessionID = Session.SessionID;
    string LoginUser = string.Empty;
    string ValidationMode = ConfigurationManager.AppSettings["ValidationMode"];
    string DebugUser = ConfigurationManager.AppSettings["DebugUser"].Trim();
    if (String.IsNullOrEmpty(DebugUser))
    {
    if (ValidationMode == "ENL")
    {
    if (Request.Headers["USERNAME"] != null)
    {
    LoginUser = Request.Headers["USERNAME"].ToString();
    }
    if (LoginUser.Contains("xyz\\") || LoginUser.Contains("xyz\\"))
    LoginUser = LoginUser.Remove(0, 5);
    }
    else if (ValidationMode == "Windows")
    {
    LoginUser = User.Identity.Name;
    string[] pos = LoginUser.Split(new string[] { "\\" }, StringSplitOptions.None);
    LoginUser = pos[pos.Length - 1].ToString();
    }
    }
    else
    {
    LoginUser = DebugUser;
    }

    if (!String.IsNullOrEmpty(LoginUser))
    {
    Session["LoginuserId"] = LoginUser;
    try
    {
    string CookieHeaders = HttpContext.Current.Request.Headers["Cookie"];

    if ((null != CookieHeaders) && (CookieHeaders.IndexOf("ASP.NET_SessionId") >= 0))
    {
    Session.Clear();

    //Response.Redirect("~/ErrorPages/SessionExpired.aspx", true);

    }
    LoggingServiceProxy.WriteMessage("Session Started at " + DateTime.Now + "Logged in user" + LoginUser);

    DataSet dtUserRole = new DataSet();
    dtUserRole = AdminServiceProxy.GetLoggedInUserRole(LoginUser);
    if (dtUserRole != null)
    {
    if (dtUserRole.Tables[0].Rows.Count > 0 && dtUserRole.Tables[0].Rows[0]["RoleName"].ToString() != string.Empty)
    {
    Session["UserContext"] = dtUserRole;
    LoggingServiceProxy.WriteMessage("User Context assigned at " + DateTime.Now);
    LoggingServiceProxy.WriteMessage(dtUserRole.GetXml());
    Response.Redirect("Home.aspx");
    }
    else
    {
    LoggingServiceProxy.WriteMessage("No records in the table");
    Response.Redirect("AuthorizationFailed.aspx");
    }
    }

    }
    catch (BodsException ex)
    {
    Response.Redirect("AuthorizationFailed.aspx", false);
    }
    catch (Exception ex)
    {
    LogManager.HandleDataException(ex);
    LoggingServiceProxy.WriteErrorMessage(Convert.ToString(ex, CultureInfo.CurrentCulture));
    }
    }
    else
    {
    LoggingServiceProxy.WriteMessage("UserPIN is empty");
    Response.Redirect("AuthorizationFailed.aspx");
    }
    LoggingServiceProxy.WriteMessage("Session start ends " + DateTime.Now);
    }

    void Session_End(object sender, EventArgs e)
    {
    LoggingServiceProxy.WriteMessage("Session timeout at " + DateTime.Now.ToString());
    Session["UserContext"] = null;
    Session.Clear();
    Session.Abandon();
    }

    When i run the application in Visual Studio.. the session start occurs immediately the session end comes ..again it goes to session start...and it goes to session end..again it comes to session start..this happens right after i run the code in Visual Studio..after that it goes to master page where we are getting values from session but there it is null..

    But i dont why it goes to session end immediately......Please help.. Sometime it goes to session start only once  and that time the application works good...

    Sunday, December 7, 2014 1:48 PM

Answers

  • User753101303 posted

    Hi,

    Does it happen when  you are not logged yet? Is the SessionID a new value every time? It is expected as long as you don't have stored something yet inside the session. Until you are doing that ASP.NET don't bother to maintain a server side session and session_start happens on each and every request.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 15, 2014 5:08 AM

All replies

  • User1550517537 posted

    Hi bsurendiran,

    I suggest that you can press F12 to debug , and check if your page has any error message.

    Best wish,

    Aswecan

    Monday, December 15, 2014 4:56 AM
  • User753101303 posted

    Hi,

    Does it happen when  you are not logged yet? Is the SessionID a new value every time? It is expected as long as you don't have stored something yet inside the session. Until you are doing that ASP.NET don't bother to maintain a server side session and session_start happens on each and every request.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 15, 2014 5:08 AM