1

I am testing to build a asp.net web application. What I have is 3 web pages:

Default.aspx;
Home.aspx;
ThirdPage.aspx;

When user submits login data to Default.aspx, I retrieve user information from db, put it in a class and add it to Context like this

HttpContext.Current.Items.Add("UserData", userData);

Then I use Server.Transfer to send the request to Home.aspx. On Home.aspx, I have a link which points to ThirdPage.aspx. I click on this link and hoped that user information would be available here as well but it is not. Where as I was hoping to retain the userdata class across the user session across all pages in my web application until users session is expired . Can some one please guide? It is possibly a beginner question so please be kind. Thanks

user1063108
  • 662
  • 1
  • 10
  • 24

2 Answers2

1

Check ASP.NET Session State Overview

Usage:

Session["UserData"] = userData;
denys-vega
  • 3,522
  • 1
  • 19
  • 24
  • I had tried this, but when I click on the link which connects to thirdpage.aspx session is lost on thirdpage.aspx, however if I do Server.transfer then session is retained. But I want to allow user to click on link and go another page within the web app and maintain its session? – user1063108 Mar 30 '15 at 02:06
  • Well I think I know the reason. I looked at the url and when I click on link, the session id in url is changed, no wonder session is null...so now question is how to retain the same session id across all pages may it be user click on a link or be transferred via Server.transfer? – user1063108 Mar 30 '15 at 02:21
0

I am unsure what the exact problem may be but I have some solutions that might work. If I am not mistaken you are trying to get user information over a couple of pages?

Here are some ways to get that done:

  1. Repeat the code you did on the first page that transfers the data to the second page and adapt it so it will work from the second to the third. This might work but I won't recommend because having to reuse and adapt the code each time you create a new page.
  2. The better one I think is to write your information to an XML file and deleting the file when the session expires. You can find how to write it here and reading it here.
  3. But my personal favorite is to have a static Class. Static classes can be acces from anywhere. As shown on the page here.

    public static class Globals

    {
        public static String name; 
        public static int age;
    
        public void SetName(){...}
        public void SetAge(){...}
        public String GetName(){...}
        public int GetName(){...}
    }
    
    • Just make it a mutable class (You can change the variables using functions) and it should be easy to get the user information across pages.

      1. Using JSON to save and load here are c# functions:

    public void SaveInfo(User user){

    //Convert to Json string json = JsonConvert.SerializeObject(user, Formatting.Indented);

    //Write to file, you will have to create a file serverside if you want
    //if you have a 
    File.WriteAllText(@"location.json", json);
    

    }

    public void LoadUser(){ using (StreamReader r = new StreamReader("Location.json")) { string json = r.ReadToEnd(); List items = JsonConvert.DeserializeObject>(json); }

Community
  • 1
  • 1
Cornelis
  • 1,065
  • 8
  • 23
  • Thanks, third option is the one I was thinking about but then I was confused for the fact that I want to retain user information only for their session, once session is over that userdata should go as well..this would be a good solution, please guide a bit more. Thanks – user1063108 Mar 30 '15 at 03:39
  • Ok I figured out my problem, I used HttpContext.Current.Session instead of Items and it seemed to have worked. I clicked this as my answer because the 3 rd option presented above was exactly what I was looking even though my implementation is completely different. Thanks a lot for your guidance – user1063108 Mar 30 '15 at 05:02
  • No problem thanx I could help. I was actually in the process of adding a forth step and will edit my code above. It's using JSON which is very easy. – Cornelis Mar 30 '15 at 05:54