ASP.NET Session is not keeping stored across pages

廉价感情. 提交于 2019-12-12 04:57:42

问题


First, I created a Login page that added a key value pair to the session and verified that on that page the session holds that pair. Next, I attempt to go to another page that looks for that pair in the session and it is not there. I've put the timeout for the session to 15000 so it won't timeout. I currently use a static class to look at the session, HttpContext.Current.Session. Each Page calls this static class to look at the session but every time the session key count = 0 except after I the pair on the login page.

public static class UserAuthenticationManager
{
    public static bool IsAuthenticated()
    {
        UserAuthenticationTicket ticket = ((UserAuthenticationTicket)HttpContext.Current.Session[DefinesPL.UserTicketSessionName]);

        string redirectUrl = String.Format(DefinesPL.LoginPage);

        if (ticket != null)
        {
            if (ticket.IsExpired())
            {
                HttpContext.Current.Session.Abandon();
                HttpContext.Current.Response.Redirect(redirectUrl, true);
            }
        }
        else
        {
            HttpContext.Current.Session.Abandon();
            HttpContext.Current.Response.Redirect(redirectUrl, true);
        }

        return true;
    }

回答1:


The most common cause is cookies being disabled. You'll need to require cookies, or switch to a cookieless session model, which is a little harder to work with.




回答2:


Might also want to make sure you have Global.asax added to your project. I believe i ran into this before, where a new session was created for every HTTP request.



来源:https://stackoverflow.com/questions/1230777/asp-net-session-is-not-keeping-stored-across-pages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!