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 630633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:52:34+00:00 2026-05-13T19:52:34+00:00

I have to do the following, when a class is instantiated I need to

  • 0

I have to do the following, when a class is instantiated I need to store that instance by user . Since I’m working in asp.net, I was wondering if I should use some of the ways asp.net provides to persist data between user requests. (Cache cant be because the data needs to be persistent and application state cant be because it needs to be specific to an user) or if I should look a way to store that info inside the class. And it needs to be persisted until I programatically say so

  • 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-13T19:52:35+00:00Added an answer on May 13, 2026 at 7:52 pm

    Session is the perfect place to store specific user data.

    Let’s assume for this answer, your class that needs to be persisted across a user session is called UserInfo. The first thing is to make sure it is marked as Serializable, so that it can be stored in Session:

    /// <summary>
    /// My custom class stored per user in Session. Marked as Serializable.
    /// </summary>
    [Serializable()]
    public class UserInfo
    {
        public string SimpleProperty
        { get; set;}
    }
    

    From code-behind or classes within the ASP.NET application’s App_Code folder, you can call session directly:

    //creating the user info as needed:
    UserInfo currentUserInfo = new UserInfo();
    
    //storing the user info instance:    
    Session["UserInfo"] = currentUserInfo;
    
    //getting the user info as needed:
    UserInfo currentUserInfo = (UserInfo)Session["UserInfo"];
    

    If in a class library / external assembly that is part of the application, referenced by the ASP.NET website /application, you can access session via HttpContext.Current.Session. Your code otherwise would be very similar:

    //creating the user info as needed:
    UserInfo currentUserInfo = new UserInfo();
    
    //storing the user info:    
    HttpContext.Current.Session["UserInfo"] = currentUserInfo;
    
    //getting the user info as needed:
    UserInfo currentUserInfo = (UserInfo)HttpContext.Current.Session["UserInfo"];
    

    When coding your class library, it’s recommended to ensure HttpContext.Current is not null prior to trying to access it and Session, as under certain circumstances it could be null.

    All of the above should meet your needs. Session by design is scoped at the user/session level so you can use the same Session key in your code. No other special requirements are needed to protect the class instance from other users/sessions. The Cache object you mentioned is not the right way to go, as it is application wide, and would require you to implement your own user-level scope to ensure UserInfo instances aren’t accidentally shared across different sessions.

    One last suggestion – you could even create a static utility/helper class which would access this information as needed, so that your code would not continually have to deal with the Session object. Quick example:

    public static class UserInfoManager
    {
        /// <summary>
        /// Gets or sets the session-scoped user info when needed.
        /// </summary>
        public static UserInfo UserInformation
        {
            get
            {
                if(HttpContext.Current != null)
                    return (UserInfo)HttpContext.Current.Session["UserInfo"];
    
                return null;
            }
            set
            {
                HttpContext.Current.Session["UserInfo"] = value;
            }
        }
    }
    

    Using the above static class, you can now easily access the instance of the class, using UserInfoManager.UserInformation, etc.

    I hope this helps.

    EDIT:

    One further suggestion. In our applications when we have to store a class instance per user like you do, we create a base Page class, which allows direct access to the class instance via a property. This may also help you keep things well refined:

    /// <summary>
    /// Base page class, which all pages in our site will inherit from
    /// </summary>
    public class MyBasePage : System.Web.UI.Page
    {
        /// <summary>
        /// Gets or sets the session-scoped user info when needed.
        /// </summary>
        protected UserInfo UserInformation
        {
            get
            {
                if(HttpContext.Current != null)
                    return (UserInfo)HttpContext.Current.Session["UserInfo"];
    
                return null;
            }
            set
            {
                HttpContext.Current.Session["UserInfo"] = value;
            }
        }
    }
    

    Then in each code-behind in the asp.net website or web application:

    /// <summary>
    /// Code-behind class for a page in my site
    /// </summary>
    public partial class SomePage : MyBasePage 
    {
        public void Page_Load(object sender, EventArgs e)
        {
            //access the user info as needed
            this.UserInformation.SimplyProperty = "something";
        }
    }
    

    As you can see, very little code is needed now to be able to access the class instance.

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

Sidebar

Related Questions

I have following code class User attr_accessor :name end u = User.new u.name =
I have the following class written by someone else that I'm trying to understand
I have the following type which I need to be instantiated by StructureMap: public
I have following class public class ButtonChange { private int _buttonState; public void SetButtonState(int
I have following class (as seen through reflector) public class W : IDisposable {
I have following UML class diagram and java coding for each, please tell me
I have following simple class: @interface Article: NSObject { NSString *title; } @property (copy,
I have following event class. I have a question related to the Property method
I have following test class @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {/services-test-config.xml}) public class MySericeTest { @Autowired
I have the following class public class MenuVeiculo { public string Nome { get;

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.