Click here to Skip to main content
15,892,072 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a web application. I stored user information to session. It is ok when I deploy it on IIS as a website. But when I deploy it on IIS as an application of Default Website, the session object is always null.
It's run under the .NET v4.5 Application pool. My IIS version is 8.0. Why Session Object is null on IIS? I'm stuck on this issue.

Thanks.

What I have tried:

public int GetUserId()
{
int userId = 0;

var userObj = (Models.User)Session["user"];

if (userObj != null)
{
userId = userObj.UserId;
}
return userId;
}


userObj is null and return userId as 0.
Posted
Updated 30-May-16 22:43pm

1 solution

You did not share the code to set the value for the session variable. Did you do this?
C#
// Create a new user and save it
Session["user"] = new User();

// Access the value here

If you did this before getting the value, only then you can get the value back from the session. This is exactly why it is recommended to use a conditional-block around the statements where you try to get the values. Also, Session variables can be removed programmatically, make sure your application is not refreshing the variables. If it is, please remove those statements. Session.Clear() etc. such functions are meant to clear the values once they are no longer needed, before the session expires.

For more discussion and overviews please read: ASP.NET Session State Overview[^]
HttpContext.Session Property (System.Web)[^]
.net - C# Clear Session - Stack Overflow[^]
 
Share this answer
 
Comments
Zan Zan Koe 31-May-16 5:26am    
Thanks for your reply.
The session object is ok when i run on IIS Express (Visual Studio).
The problem is the Session object is null once deployed the application to IIS 8.
F-ES Sitecore 31-May-16 5:33am    
Then there is a problem with your IIS setup\configuration. Google "iis8 session" and go through some of the other problems\solutions people have had.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900