0

My goal here is to properly assign a session and retrieve the value stored in that session.

When users come to my first page, a Default.aspx page, I set the session in the code behind.

HttpContext.Current.Session["permissions"] = "Super";

However, I am unable to access this section in a Data Access Class in another file. Am I doing something wrong, or does anyone know a correct way of accessing an already set session from a C# class?

I try to access the session using the same syntax: String permission = HttpContext.Current.Session["permissions"].ToString();

Garrett
  • 1,658
  • 2
  • 17
  • 29
  • 5
    I don't think accessing session is data layer's concern. Why not reconsider the architecture? You could use dependency injection instead, or pass a variable back and forth – Dimitri Jul 25 '12 at 19:12

1 Answers1

1

I am pretty sure , that you can always override this situation. What you are trying to do is not considered a good design principal.

what you can do is to pass the CurrentUser and/or his/her role to the data class by populating a custom property on that class. Within that class you can use the value of this property to work on the user's role.

let me know , if this helps you.

For code samples , you can always look at this SO question

How to access session variables from any class in ASP.NET?

Community
  • 1
  • 1
Gagan
  • 5,416
  • 13
  • 58
  • 86
  • The linked class is a much more robust way of doing it. As I was implementing an asmx web service, I did need to enable the SessionState flag as well. Thanks – Garrett Jul 25 '12 at 21:58