gpt4 book ai didi

c# - SessionManager 和 Session 随机为 null

转载 作者:太空宇宙 更新时间:2023-11-03 16:03:25 26 4
gpt4 key购买 nike

几个月来我一直面临这个问题,我已经阅读了几乎所有关于此的信息并实现了大多数解决方案,但仍然没有任何改变。我不知道我在哪里犯了错误。

我正在使用自定义 SessionManager 类在我的 ASP.net CMS 网站的管理面板中轻松获取/设置值到 Session 中。当用户登录时,我将用户数据存储到 Session,然后读取 Admin.master 页面以检查用户是否已登录。在不同的服务器和本地主机上,SessionManager.CurrentUser value 在随机时间为 null,登录后有时 2 分钟有时 20 分钟,无论页面是否空闲。我所有的网站都有同样的问题。

我的SessionManager.cs是

public class SessionManager
{
public SessionManager() { }

public static User CurrentUser
{
get { return (User)HttpContext.Current.Session["crntUsr"]; }
set { HttpContext.Current.Session["crntUsr"] = value; }
}

public static string CurrentAdminLanguage
{
get
{
if (HttpContext.Current.Session["crntLang"] == null) HttpContext.Current.Session["crntLang"] = SiteSettings.DefaultLanguage;
return HttpContext.Current.Session["crntLang"].ToString();
}
set
{
HttpContext.Current.Session["crntLang"] = value;
}
}
}

注意:用户类是[Serializable]

在Admin.master Page_Load

if (SessionManager.CurrentUser == null) Response.Redirect("../login");

在 web.config 中

    <system.web>
<sessionState mode="InProc" customProvider="DefaultSessionProvider" cookieless="UseCookies" regenerateExpiredSessionId="true" timeout="60"/>
<machineKey validationKey="CC0...F80" decryptionKey="8BF...1B5" validation="SHA1" decryption="AES"/>
<authentication mode="Forms">
<forms loginUrl="~/login" timeout="60" slidingExpiration="true" cookieless="UseCookies" />
</authentication>

<system.webServer>
<modules>
<remove name="Session"/>
<add name="Session" type="System.Web.SessionState.SessionStateModule"/>
</modules>

我真的没有更多的想法来解决这个问题。请帮助:(

最佳答案

您是否检查过您的应用程序池回收超时?这是 session 比预期提前“消失”的常见问题。 checkin IIS

关于c# - SessionManager 和 Session 随机为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20426300/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com