asp.net mvc 会话状态..有帮助吗?

发布于 2024-08-05 11:40:49 字数 298 浏览 2 评论 0原文

有没有人经历过会话在应用程序范围内共享?

我的 MVC 应用程序具有普通设置,但由于某种原因,我的会话正在共享。

我并没有真正想过什么时候我可以在 FF 和 IE 之间切换并保持登录状态,但现在,我注意到我也可以切换机器。

我的 web.config 没有任何内容来设置会话状态,所以我假设它是基于 cookie 的,但看起来并非如此。

以前有人经历过这种情况吗?如果有,你是如何解决的?

仅供参考:我在 Server 2003 IIS6 上运行它。

谢谢大家!!

加夫

Has anyone ever eperienced session's being shared application wide?

My MVC application has the vanilla setup but for some reason, my sessions are being shared.

I didn't really think about it when I could switch between FF and IE and maintain a logged in state but now, I've noticed that I can switch machines too.

My web.config doesn't have anything in to setup the session state, so I assumed it was cookie based but it seem it's not.

Has anyone ever experienced this before and if so, how did you resolve it?

FYI: I'm running it on Server 2003 IIS6.

Thanks all!!

Gav

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

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

发布评论

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

评论(2

梦冥 2024-08-12 11:40:49

您是否专门在 Session 中使用存储内容,或者您​​是否在 TempData 调用(也暂时使用会话)中看到这一点?

Are you specifically using storing things in Session or are you seeing this in TempData calls (which temporarily uses session as well)?

若相惜即相离 2024-08-12 11:40:49

你相信吗...愚蠢的静态变量...

我认为通过使用静态私有变量,它可以帮助我在获取数据时不做太多工作,但看起来,这是邪恶的根源。 (邪恶博士小指)

谢谢大家!

** 注意这不是!去做 **

public class UserHelper
{
    private static UserSession _session;
    public static UserSession Session
    {
        get
        {
        // If we already have the session, don't get it
        // from the session state
        if (_session == null)
        {
            // Attempt to get the session from the
            // session state
            _session = GetUserSessionFromSession(HttpContext.Current.Session);
            if (_session == null)
            {
            // Create a new session object
            _session = new UserSession();
            }
        }
        return _session;
        }
        set
        {
        // Set the local value
        _session = value;
        // Add the object to the session state
        HttpContext.Current.Session["SMEUser"] = _session;
        }
    }

    public static void Logout()
    {
        Logout(HttpContext.Current.Session);
    }

    public static void Logout(HttpSessionState session)
    {
        _session = null;
        session.Clear();
    }

    public static UserSession GetUserSessionFromSession(HttpSessionState session)
    {
        // Get the session from the session state
        UserSession us = session["SMEUser"] as UserSession;
        return us;
    }
}

Well would you believe it... Stupid static variables...

I thought by using a static private variable that it would help me by not doing as much work when getting the data, but as it seems, it was the root of evil. (doctor evil pinky)

Thanks everyone!

** NOTE THIS IS HOW NOT!! TO DO IT **

public class UserHelper
{
    private static UserSession _session;
    public static UserSession Session
    {
        get
        {
        // If we already have the session, don't get it
        // from the session state
        if (_session == null)
        {
            // Attempt to get the session from the
            // session state
            _session = GetUserSessionFromSession(HttpContext.Current.Session);
            if (_session == null)
            {
            // Create a new session object
            _session = new UserSession();
            }
        }
        return _session;
        }
        set
        {
        // Set the local value
        _session = value;
        // Add the object to the session state
        HttpContext.Current.Session["SMEUser"] = _session;
        }
    }

    public static void Logout()
    {
        Logout(HttpContext.Current.Session);
    }

    public static void Logout(HttpSessionState session)
    {
        _session = null;
        session.Clear();
    }

    public static UserSession GetUserSessionFromSession(HttpSessionState session)
    {
        // Get the session from the session state
        UserSession us = session["SMEUser"] as UserSession;
        return us;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文