会话超时不起作用ASP.NET 4

时间:2012-07-30 21:41:24

标签: asp.net forms session authentication

我们很难让Session Timeout在ASP.NET 4中运行。我们将超时设置为720分钟(12小时)。我们正在使用表单身份验证无论我将超时设置为什么,大约20分钟后都会发生超时。我确定我们配错了,但我不确定是什么。我已经在线查看了几个修复程序(标题等),但似乎无法正常工作。这是我们的配置文件:

<authentication mode="Forms">
     <forms loginUrl="~/Login.aspx" name="CAFormsAuth" timeout="720" slidingExpiration="true" defaultUrl="Default.aspx" /> </authentication> <authorization>    <!--<allow users="*"/>-->   <deny users="?" /> </authorization>

这是我们的登录代码:

try
        {
            string adLogin = txtUserName.Text;

            bool isValid = false;
            //Validate User against AD First...
            //----------------------------------
            try
            {
                isValid = AuthenticateUser(cboDomains.Text, adLogin, txtPassword.Text);
            }
            catch (Exception ex)
            {
                //Should read "Invalid Username or Password"...
                //lblError.Text = ex.Message;
                lblError.Text = "Invalid User Name or Password.";
                return;                   
            }

            //Now, See if the user exists in the database.
            //---------------------------------------------
            db_users user = null;
            try
            {
                user = usrHelp.GetUserByADUserName(cboDomains.Text + @"\" + adLogin);

                if (user == null)
                {
                    lblError.Text = "User " + adLogin + " does not exist in the database.";
                    return;
                }
            }
            catch (Exception ex)
            {
                ErrorLoggingHelper.LogToSource(Globals.ApplicationName, ex.ToString(), System.Diagnostics.EventLogEntryType.Error);

                lblError.Text = "User " + adLogin + " does not exist in the database.";
                return;
            }

            if (isValid)
            {
                if (chkRemember.Checked)
                {
                    SetCookies();
                }
                else
                {
                    RemoveCookie();
                }
            }
            else
            {
                lblError.Text = "Password is not valid";
                Telerik.Web.UI.RadAjaxManager.GetCurrent(this.Page).ResponseScripts.Add(String.Format("SetFocus('{0}')", txtPassword.ClientID));
                return;
            }

            Session[Globals.LoggedInUserName] = txtUserName.Text;
            Session[Globals.LoggedInUserId] = user.user_id;
            Session["CurrentUser"] = user;

            FormsAuthentication.RedirectFromLoginPage(txtUserName.Text, false);

        }
        catch (Exception ex)
        {
           //deleted error logging code here...
            lblError.Text = "Error authenticating user. Please contact the administrator.";
        }

2 个答案:

答案 0 :(得分:2)

IIS中的应用程序池级别中存在高级设置。 “处理模式”下有“空闲超时”设置,默认设置为20分钟。你应该可以在那里改变它。

答案 1 :(得分:0)

尝试在web.config中设置会话超时,如下所示:

<configuration>
  <system.web>
    <sessionState mode="InProc" timeout="720" />
  </system.web>
</configuration>