ASP.net page_load未检测到会话 [英] ASP.net page_load doesn't detect session

查看:61
本文介绍了ASP.net page_load未检测到会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 page_load 中使用 session 变量时遇到问题.我有一个页面( members.aspx ),它是一个成员区域.

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

如果用户未登录,它将显示一个表单,要求他们登录.表单提交其详细信息,检查是否正常,然后如果设置为 OK ,则设置 session 变量.然后重新加载页面.

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.

因此,如果用户已正确验证身份,它将设置 Session ["memberLoggedIn"] = true .然后,我的 page_load 函数看起来像这样

So if the user has authenticated correctly, it sets Session["memberLoggedIn"] = 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");
    }
}

还有更多代码,但是本质上,应该写"OK".但是,它没有出现.即使设置了 session .如果我直接转到页面,它将显示.仅用于从初始登录重新加载成员页面,从而停止该页面.

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.

有什么想法吗?!

======================设置会话的代码是

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

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

然后我只检查我所有页面上的(Convert.ToBoolean(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....)

推荐答案

尝试一下

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");
            }
    }

这篇关于ASP.net page_load未检测到会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆