MVC 应用程序中的会话过期问题

Session Expire issue in MVC application

我已经创建了 mvc 应用程序。我已经编写了更新代码 web.config.i.e

try
{
    //// Helps to open the Root level web.config file.
    Configuration webConfigApp = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
    AppSettingsSection objAppsettings = (AppSettingsSection)webConfigApp.GetSection("appSettings");
    //Edit
    if (objAppsettings != null)
    {
        ////Modifying the AppKey from AppValue to AppValue1
        objAppsettings.Settings["DaysToKeepJob"].Value = model.Keepjobsfolders.ToString();
        objAppsettings.Settings["DeleteLocalStoreStudies"].Value = model.DeleteLocalStoreStudies.ToString();
        objAppsettings.Settings["ManualBurnerNoOfCopies"].Value = model.ManualNumberofCopies.ToString();
        objAppsettings.Settings["DefaultBurner"].Value = model.DefaultBurner.ToString();
        objAppsettings.Settings["AutoBurnerNoOfCopies"].Value = model.AutoNumberofCopies.ToString();
        objAppsettings.Settings["SmartDisk"].Value = model.chkSMARTDisk.ToString();
        objAppsettings.Settings["ForcetodefaultTransfer"].Value = model.chkKeepjobsfolders.ToString();
        ////Save the Modified settings of AppSettings.
        webConfigApp.Save();
    }
    if (diskType.Title != null)
    {
        var res = DiskTypeSave(diskType);
    }
    return Json(new { Status = "Success", Msg = "Rules Saved Successfully." }, JsonRequestBehavior.AllowGet);
}

但在这一行之后 webConfigApp.Save(); 我的会话即 Session["UserAccessRights"] 得到 null .我知道因为更新 web.config 它变得空了。

请建议保持会话甚至 web.config 更新

为此,您首先必须将 Sessions 复制到局部变量中,然后再次分配。但是为此你还必须保留 Session.SessionId 副本,因为在新会话中它会再次设置。

每次更改 web.config 应用程序都会在 IIS 中重新启动。

您可以执行以下操作之一:

  1. 使用另一个设置文件 xml、json 等来存储这些值
  2. 使用数据库为每个用户存储这些设置
  3. 使用 SQL 服务器保存会话或状态服务器,而不是内存中(默认)