简体   繁体   中英

ASP.NET Sessions in C#

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();

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM