Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6864215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:53:01+00:00 2026-05-27T02:53:01+00:00

I want a certain segment of code to be executed only once per session,

  • 0

I want a certain segment of code to be executed only once per session, so if the user navigates to the page containing this code while within the same session, I don’t want this segment to be executed. The code lives in a default page that is called right after login, and user can navigate to this default page from other pages.

My Solution:

if (Session["MySessionName"] == null && ConditionTwo) //initially MySessionName won't exist so the if condition resolves to true
{
    //Assign Value to MySessionName so the if condition resolves to false next time
    Session["MySessionName"] = "MySessionValue";
    //Do Something
}

My Problem:

This works fine on visual studio, after deploying to test server this solution doesn’t seem to work all the time. If I log out and log back in, the condition Session["MySessionName"] == null returns false and the code segment won’t be executed. However if I close the browser, reopen, and log in, it works fine. I have checked if the logout did a proper session dispose and it does.

I also tried,

HttpContext.Current.Session["MySessionName"] = "MySessionValue"; 
//and 
Session.Add("MysessionName", true);

but result was the same.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:53:01+00:00Added an answer on May 27, 2026 at 2:53 am

    If you are using forms authentication, you need to call FormsAuthentication.SignOut().

    In addition, on logout you need to call Context.Session.Abandon() to terminate the current session.

    I also make sure that I always explicitly clear any sensitive or user-related values from the session state prior to abandoning the session.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a situation where I want certain code to be executed no matter
I'm doing a live editing thing, but only want certain portions of the page
I want certain functions in my application to only be accessible if the current
I want to test the behavior of a certain piece of .NET code in
I have a class that loads certain parts of my page. What I want
I am creating an infopath form in 2003 where I only want certain sections/controls
Quick question: Is there a per-user data storage object (similar to Session ) that
I want that certain mysql query would be executed in 1 minute after mysql_query()
Basically I want a certain spot on my page to be clicked when the
Would it be safe to include controls you only want for certain users in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.