Click here to Skip to main content
15,893,486 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I am developing an application architecture that uses 2 sub projects: a) asp.net web application (it covers user interface and business logic) and b) class library. (it covers data access layer)

After system user successfully logs in , the user information is stored in a session object.

The problem i am facing is when i try to access that session object in class library project(data access layer), it always returns null.

I need to access the session object in class library project because, in my case each user has their own username and password for database access(for security reasons);

So, How how do i read and write from/to session object in class library project
Posted
Comments
StianSandberg 13-Aug-12 5:36am    
You'll have to add System.Web reference to your project. Then access HttpContext System.Web.HttpContext.Current

might help
C#
using System.Web.SessionState;
/// <summary>
/// Summary description for Class1
/// </summary>
public class Class1:IRequiresSessionState
{
    public Class1()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    public object GetSession(string key) {

         object sessionKey = HttpContext.Current.Session[key];
         return sessionKey;
    }
}
 
Share this answer
 
Comments
Dylan Morley 13-Aug-12 6:00am    
from OP: thanks raju melveetilpurayil, but it didn't worked
raju melveetilpurayil 13-Aug-12 6:25am    
it should work, please check the whole sample
class lib
<pre>
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.SessionState;
namespace ClassLibrary1
{
public class SessionHandler:IRequiresSessionState
{
public SessionHandler() { }
public object GetSession(string key) {
object session = HttpContext.Current.Session[key];
return session;
}
public void SetSession(object data,string key) {
HttpContext.Current.Session.Add(key, data);
}

public string Test(string sessionKey){

string ss = GetSession(sessionKey).ToString();
return ss + " from ClassLibrary1";
}

}
}
</pre>

test page
<pre>
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ClassLibrary1;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SessionHandler s = new SessionHandler();
s.SetSession("TestSession", "skey");

Response.Write(s.GetSession("skey").ToString());
Response.Write(s.Test("skey"));
}
}
</pre>

Output
<pre>
TestSessionTestSession from ClassLibrary1
</pre>
Punjabi Nikki 20-Jun-16 14:23pm    
Awesome. It works like charm!
Thank You.
Member 13139151 23-Mar-22 8:45am    
واللهى انت راجل محترم
kevinkwtun 10-Aug-17 2:48am    
Cool, that works for me too. Thanks mate.
thank u very much raju melveetilpurayil,i tried your code and it works quite well for the scenario i questioned about.
but now, i need to implement wcf layer between web project and class library project.

Now My web application(User Interface) calls wcf service(My services/functions) and wcf interacts with the class library(data access layer).

Web Application <=====> WCF <=====> Class Library <=====> Database


how do i achieve the same thing that you solved for previous scenario.
 
Share this answer
 

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