Search code examples
asp.netajaxsessioncookieswebusercontrol

How to create a random user with cookie without log in


Hi I have a shopping web site and I want to create random user per computer with cookie or session But I can not create randomly. Could you guys help me to create a user per computer then add items to basket. I have basket codes Just need to create user per pc without log in...

Here is how do I create random Session ID

>> public void generate(){
>> SessionIDManager Manager = new SessionIDManager();
>> string NewID = Manager.CreateSessionID(Context);
>> userId.Text = NewID.ToString();
>> }

I call this in page load i say if(!ispostback){generate()}

so now when i refresh website id also gets refresh How can I control it if its same pc hold the session Id ?


Solution

  • Try serialising the basket object and placing that into a cookie. When the user performs a checkout then create a user if needed and convert cookie basket to order in db.

    You will need to start by trying to write some code though, then come back if you're still stuck.

    UPDATE

    The sessionId is created for each browser session, if you close the browser then reopen it you'll get a new sessionId, using the sessionId to identify a user is only going to work as long as the user doesn't close the browser. You could store the sessionId in a cookie and check the cookie when a user returns

    HttpCookie myCookie = new HttpCookie("UserId");
    myCookie["SessionId"] = NewID.ToString();
    myCookie.Expires = DateTime.Now.AddDays(30);
    Response.Cookies.Add(myCookie);
    

    But personally I think you should look at this from a different angle.

    UPDATE 2

    Try this, the sessionId will be the same when you click refresh until you close the browser.

    public void generate(){
        var sessionId = Session.SessionID;
        userId.Text = sessionId;
    }