开发者

ASP.net page_load doesn't detect session

I have a problem with using session variable in the page_load. I have a page (members.aspx) which is a members area.

If the user isn't logged in, it will display a form asking them to login. The form submits their details, checks if ok then sets session variables if OK. It then reloads the page.

So if the user has authenticated correctly, it sets Session["membe开发者_JAVA百科rLoggedIn"] = true. My page_load function then looks like this

protected void Page_Load(object sender, EventArgs e)
{
    if (Convert.ToBoolean(Session["memberLoggedIn"]) == true) {
        Response.Write("OK");
    }
}

There is more code, but essentially, the "OK" should be written. However, it doesn't appear. Even though the session IS set. If I go to the page directly it will then show. It's just for the reloading of the members page from the initial login which stops it.

any ideas?!

====================== the code to set the session is

if (logindetails.Count > 0) {
    System.Data.DataRow details = logindetails[0];
    Session["memberLoggedIn"] = true;
}

I then just check if (Convert.ToBoolean(Session["memberLoggedIn"]) == true) on all my pages. To be honest it doesn't seem that reliable and I Think sometimes I need to understand the order in which pages are loaded as when I destroy the session on the log out page, some parts still show the logged in features! (but that's another story....)


try this

protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            var user = HttpContext.Current.User;
            if(user.Identity.IsAuthenticated)
            {
                 Session["memberLoggedIn"] = true;
                 Print();
            }
        }
    }

    public void Print()
    {
            if (Convert.ToBoolean(Session["memberLoggedIn"]) == true)
            {
                Response.Write("ok");
            }
    }


This sounds like when you log in you are choosing whether to write "OK" before you have processed the login. In terms of the page's pipeline page_load is before your event handler that you are likely processing the login.

You have various options, the simplest of which could be to move this particular logic to Page_PreRender (if it makes sense to) as at this point your session variable will have been set.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜