Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
hi, friend.
I am new in mvc. and i have done application in it. but following problem occurs in session:
Here’s a list of different options that I have tried so far to solve the problem of session management in the site. There are basically 3 variations that I used. I am going to explain all of them in detail. How I implemented the code for it and what is the problem that is caused by that code.
The very first method is StateServer method which I have defined the session type in web.config file. A snippet of that definition is here:
XML
<sessionstate mode="StateServer">
     sqlConnectionString="data source=xxx.91.198.xxx:42424;initial catalog=hrprowlx_pmsnew;user id=xxx;password=xxx;"
     timeout="500000"         />

Another file that is used is Accountcontroller.cs which is used to store the values in the session that we defined in the web.config file.
[code]
C#
[HttpPost]
        public  ActionResult LogOn(LogOn lg)
        {


            var strQuery = (from u in new ProjectManagementSystemEntities3().user_master
                            where u.user_login_name == lg.UserName && u.user_password == lg.Password
                            select u.user_password).SingleOrDefault();

            if (strQuery != null)
            {
                var id = (from u in pb.user_master
                          where u.user_login_name == lg.UserName
                          select new { u.user_id }).FirstOrDefault();
                SimpleSessionPersister.Username = Convert.ToString(id.user_id);
                                 // here SimpleSessionPersister static class of  securitymodel.cs file


                session_master sm = new session_master();
                if (sm != null)
                {
                    sm.user_id = id.user_id;
                    sm.session_created = DateTime.Now;

                }
                if (ModelState.IsValid)
                {
                    pb.session_master.AddObject(sm);
                    pb.SaveChanges();
                }
                return RedirectToAction("Welcome", "Home");
            }
            else
            {
                return View();
            }
}

The session is accessed in these two files: Project.controller.cs and Accountcontroller.cs
[code]
C#
string id1 = SimpleSessionPersister.Username;

// here SimpleSessionPersister static class of given securitymodel.cs file

After implementing this method when I run the project I get the following error message on the remote server. The same code runs without any errors on my local server.
Unable to make the session state request to the session state server. Please ensure that the ASP.NET State service is started and that the client and server ports are the same. If the server is on a remote machine, please ensure that it accepts remote requests by checking the value of HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\aspnet_state\Parameters\AllowRemoteConnection. If the server is on the local machine, and if the before mentioned registry value does not exist or is set to 0, then the state server connection string must use either 'localhost' or '127.0.0.1' as the server name.

Now here is the second method that I used to solve the session problem. This is the SQL server method which is also defined in the web.config file. It should be noted that I am creating a database to use this type of session.
2) Session mode="SQLServer"
XML
<sessionstate mode="SQLServer" sqlconnectionstring="SqlState" allowcustomsqldatabase="true" cookieless="false" timeout="10" />

Other files that are used to set values for the session and to access the session are same as I described in the first method.
Works fine on my local server but on remote server this method gives me the following error.
Failed to login to session state SQL server for user 'MD-PLESK-WEB2\IWPD_286(hrprowlx)'.

The third and last alternative that I used is the In Process method. This is the code snippet used in the web.config file to define the session.
3). Session mode="InProc"
XML
<sessionstate mode="InProc" customprovider="DefaultSessionProvider" timeout="500000" />

The other two files to use the session as same as used in the previous two methods. This last method is not generating any errors but instead there is a strange behavior. The session gets timed out in 30 seconds automatically. I set the value for time out specifically more than that but still it doesn’t take that time and expires in 30 seconds.
thank you.
Posted
Updated 24-May-12 2:33am
v2
Comments
Raman Ghantiyala 6-Sep-12 2:02am    
session_master sm = new session_master();
where did you create it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900