2

I want to use Session values from static properties of a static class which is declared in another referenced project. Some of the properties are per application and some are per user:

public class MyClass
{
    public static string var1 { get; set; } //Always static
    public static string var2 { get; set; } //Need to use Session[Var2]
    public static string var3 { get; set; } //Always static
    public static string var4 { get; set; } //Need to use Session[Var4]
}
Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
Ramesh
  • 1,041
  • 15
  • 39

2 Answers2

3
public class MyClass
{
  public static string var2 
  {
     get { return (string)System.Web.HttpContext.Current.Session["KEY_VAR2"]; }
     set { System.Web.HttpContext.Current.Session["KEY_VAR2"] = value; } 
  }
}
Igor
  • 15,833
  • 1
  • 27
  • 32
0

If I understood your question correctly, you want to store session values of the all users in a collection. So, the session is provided for per user. You can create a static dictionary for per user and store the session values.

public class MyClass
{
    public static string var1 { get; set; } //Always static
    public static Dictionary<string,string> var2Dict = new Dictionary<string, string>();
    public static string var3 { get; set; } //Always static
    public static Dictionary<string, string> var4Dict = new Dictionary<string, string>();
}

Then add the session value for user to dictionary;

MyClass.var2Dict.Add(HttpContext.Current.User.Identity.Name, (string)HttpContext.Current.Session["var2"]);
lucky
  • 12,734
  • 4
  • 24
  • 46