How to access ASP.Net Session object outside the web project (class library)
I am implementing a custom membership and role providers where I need to store all the role/membership information in the user's sess开发者_如何学编程ion.
I am implementing these custom providers inside a class library project (different from the website project) and need to access the session in them. The idea is to store the role/membership related information in the session after retrieving them for the first time from the database.
When I try to access the Session using
System.Web.HttpContext.Current.Session
I get this as a null object (Object reference not set to an instance of an object.
Why is the session turning out to be null?
Could be if you have sessions turned off, in a handler for example or maybe the page has a setting for not using session.
Otherwise it should normally return a session object, I have used the same solution in my own projects.
But you should always have code to test for null pointers, just in case.
Have you tested that you get a HttpContext at all?
精彩评论