views:

39

answers:

1

I am implementing a custom membership and role providers where I need to store all the role/membership information in the user's session.

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?

A: 

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?

David Mårtensson
Yes, I do get the HTTP context object and other properties do have relevant values. Just the session turns out to be null. At what point in the page life cycle does the session object becomes available? Could it be that this code is getting invoked before than?
Moiz Tankiwala
Possibly but unlikely. This page describes the page lifecycle including where HttpContext is created http://msdn.microsoft.com/en-us/library/bb470252.aspx and I cannot se where you could have code running before HttpContext is populated completely.
David Mårtensson