会话变量

发布于 2024-11-25 22:52:28 字数 556 浏览 0 评论 0原文

我定义了我的会话变量,在我的 Global.asax 中称为 user ,如下所示

    protected void Session_Start(object sender, EventArgs e)
    {
        Session["idMap"] = "";
        Session["user"] = "";
    }

我还有一个 asp:Login 在其中设置值loggedIn 事件中的会话变量

    protected void lgnMapZone_LoggedIn(object sender, EventArgs e)
    {
        Session.Abandon();
        Session["user"] = lgnMapZone.UserName;
    }

我的问题是,当用户进行身份验证时,它会调用 void session_start 并删除我的变量,我该如何解决这个问题?

I defined my session variable, in my Global.asax called user like this

    protected void Session_Start(object sender, EventArgs e)
    {
        Session["idMap"] = "";
        Session["user"] = "";
    }

Also i have a asp:Login in where i set the value of the session variable in the event of loggedIn

    protected void lgnMapZone_LoggedIn(object sender, EventArgs e)
    {
        Session.Abandon();
        Session["user"] = lgnMapZone.UserName;
    }

My problem it's that when the users it's autenthicated, the void session_start it's called and erase my variable, how can i solved this??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

红墙和绿瓦 2024-12-02 22:52:28

Session.Abandon() 销毁会话并触发 Session_OnEnd 事件。当用户注销而不是登录时,您应该调用此方法。

您可能应该调用 Session.Clear(),它只是在登录时从会话对象中删除所有值(内容)。

这将解决您现在遇到的问题。

Session.Abandon() destroys the session and the Session_OnEnd event is triggered. You should call this method when the user does a logout instead of log in.

You should probably call Session.Clear() which just removes all values (content) from the session Object while logging in.

This will resolve the issue that you have now.

〃温暖了心ぐ 2024-12-02 22:52:28

在 Session_Start 中再次设置变量之前,您可以检查该变量是否已设置。

You could check whether the variable is set or not before you set it again in Session_Start.

梦中楼上月下 2024-12-02 22:52:28

简单:

在 Session_Start 内:

if (Session["user"] != "")
    // Do something else
else
    // Set Session["user"] = "", etc

Easy:

inside Session_Start:

if (Session["user"] != "")
    // Do something else
else
    // Set Session["user"] = "", etc
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文