views:

384

answers:

2

I am new to ASP.NET. I am creating a website in which i have to create session for every user who gets login into the site. Now I am not been able to picturize how to store session in my database.

I read few books for the same and got how to configure Session state. But i m still at the same place where i started. I am not able to get where should i save session id. I m using sql server session state. I have created database aspnetdb and all tables r over their. But i dont know how to use them. How to save my session id into database. I m totally confused how to explain my problem.

+3  A: 

See HOW TO: Configure SQL Server to Store ASP.NET Session State

Basically, you have to configure it in web.config:

<sessionState 
            mode="SQLServer"
            sqlConnectionString="data source=127.0.0.1;user id=<username>;password=<strongpassword>"
            cookieless="false" 
            timeout="20" 
    />
Andomar
thx for help. But I have done this too. But still I dont know how does it work. how my session id is maintained into database. Even i dont know whether users session will get maintained or not by doing this. I have even written machine key in web config file but dont know how all this is working. I m not able to visualize. Plz help considering me as a newbie in this context
Shantanu Gupta
+2  A: 

You don't need to store the session ID in the database -- that part is done for you by the runtime, assuming that you're using sessions in SQLServer mode.

The Session provider will read the session data from the DB at the beginning of each request, and write it at the end of each request. It does this by setting a cookie that contains the session ID, and then using the value of the cookie as the key for the DB lookup it does to find the rest of the session data.

You can always access the session ID from the Session.SessionID property, but you should rarely need to do so.

The Session ID cookie is not sent to the client (nor is the session data saved in the DB) until the first time you save something in the Session object. You can see this easily with a web debugger such as Fiddler.

FWIW, normally logins should not be handled using session state. The standard ASP.NET Membership provider, for example, uses a parallel system that's based on cookies.

RickNZ