增加asp的会话时间

时间:2017-10-25 12:26:48

标签: c# asp.net session authentication

在我的代码中我在身份验证模式下使用“表单”为我的“Remmember Me”CheckBox而且我想要增加我的会话TimeOut。 我设置我的会话TimeOut在“21600”分钟,所以我设置我的Cookie到期时间相似的会话时间。 但它不起作用......

这是我的Web.Config代码

<system.web>
<compilation debug="true" targetFramework="4.5"/>
<httpRuntime targetFramework="4.5"/>
<authentication mode="Forms">
  <forms loginUrl="~/Index.aspx" defaultUrl="~/Page/Dashboard.aspx?tab-1" name=".STAUTHD" cookieless="UseCookies" slidingExpiration="true" protection="All" requireSSL="false" timeout="21600" path="/" enableCrossAppRedirects="false" />
</authentication>
<sessionState mode="InProc" timeout="21600" cookieName="ds_albama"  />
<machineKey validationKey="D50B5C89CB21F4F1422FF158A5B42D0E8DB8CB5CDA1742572A487D9401E3400267682B202B746511891C1BAF47F8D25C07F6C39A104696DB51F17C529AD3CABE" decryptionKey="8A9BE8FD67AF6979E7D20198CFEA50DD3D3799C77AF2B72F" validation="SHA1" />
<membership defaultProvider="OdbcProvider" userIsOnlineTimeWindow="30">
  <providers>
    <add name="OdbcProvider" applicationName="StoreD" type="StoreDashboard.App_Code.OdbcMembershipProvider" connectionStringName="OdbcServices" enablePasswordRetrieval="true" enablePasswordReset="true" requiresQuestionAndAnswer="false" writeExceptionsToEventLog="false" requiresUniqueEmail="false" />
  </providers>
</membership>
<customErrors mode="Off" defaultRedirect="~/Error/ErrorGeneral.html">
  <error statusCode="403" redirect="~/Error/Error403.html" />
  <error statusCode="404" redirect="~/Error/Error404.html" />
  <error statusCode="500" redirect="~/Error/Error500.html" />
</customErrors>
<authorization>
  <deny users="?"/>
</authorization>

这是我的Index.aspx代码

<asp:Login ID="Login1" runat="server" RenderOuterTable="false" FailureAction="Refresh" FailureText="نام‌کاربری و یا رمز عبور صحیح نیست" OnLoggedIn="Login1_LoggedIn">
        <LayoutTemplate>
            <asp:UpdatePanel runat="server" ID="updLogin">
                <ContentTemplate>
                    <div class="wrapper">
                        <div class="login">
                            <h2>ورود کاربران</h2>
                            <label for="">
                                نام کاربری
                            </label>
                            <asp:TextBox ID="UserName" runat="server" title="ایمیل" ValidationGroup="ctl00$Login1" oninvalid="this.setCustomValidity('ایمیل را صحیح وارد کنید')" oninput="setCustomValidity('')"></asp:TextBox>
                            <label for="">
                                کلمه‌ی عبور
                            </label>
                            <asp:TextBox ID="Password" runat="server" title="کلمه عبور" TextMode="Password" ValidationGroup="ctl00$Login1" oninvalid="this.setCustomValidity('رمز عبور را صحیح وارد کنید')" oninput="setCustomValidity('')"></asp:TextBox>
                            <label class="checkbox">
                                <asp:CheckBox ID="RememberMe" runat="server" Text="<div class='control-indicator'></div>مرا بخاطر بسپار" TextAlign="Right" />
                            </label>
                            <%--<asp:LinkButton ID="LoginButton" runat="server" CommandName="Login" Text="ورود" ValidationGroup="ctl00$Login1" CssClass="button" />--%>
                            <asp:Button ID="LoginButton" runat="server" CommandName="Login" Text="ورود" ValidationGroup="ctl00$Login1" CssClass="button" />
                            <asp:Literal ID="FailureText" runat="server" EnableViewState="False"></asp:Literal>
                        </div>
                    </div>
                </ContentTemplate>
            </asp:UpdatePanel>
        </LayoutTemplate>
    </asp:Login>

这是我的Index.aspx背后代码

protected void Login1_LoggedIn(object sender, EventArgs e)
    {

        try
        {
            string username = Login1.UserName;
            BlUser oBlUser = LoadUserInfo(username);

            FillWebInfo(oBlUser);
            PubFunc.SaveLog("LogIn", "user LogIn with username: " + username, Information.UserID, PubFunc.GetUserIP());
        }
        catch (Exception ex)
        {
            PubFunc.SaveLog("Exception", "Exception In Index Of Dashboard :: Login1_LoggedIn: " + ex.Message, "-1", PubFunc.GetUserIP());

            FormsAuthentication.SignOut();
            Session.Clear();
            FormsAuthentication.RedirectToLoginPage();
        }
    }

    private void FillWebInfo(BlUser oUser)
    {
        Information.Name = oUser.Name + " " + oUser.Family;
        Information.UserID = oUser.UserID;
        Information.Username = oUser.Username;
        Information.IsAdmin = oUser.IsAdmin;
        Information.IsCustomer = oUser.IsCustomer;
        Information.IsExpert = oUser.IsExpert;
        Information.IsIntermediate = oUser.IsIntermediate;
        Information.IsSupplier = oUser.IsSupplier;
        Information.IsOperator_AR = oUser.IsOperator_AR;
        Information.IsOperator_CH = oUser.IsOperator_CH;
        Information.IsOperator_DE = oUser.IsOperator_DE;
        Information.IsOperator_EN = oUser.IsOperator_EN;
        Information.IsOperator_FR = oUser.IsOperator_FR;
        Information.IsOperator_RU = oUser.IsOperator_RU;
        Information.IsOperator_TR = oUser.IsOperator_TR;
        Information.IsWebSupporter = oUser.IsWebSupporter;
        Information.IP = PubFunc.GetUserIP();

        if (Login1.RememberMeSet)
        {
            MakeValid(Information.Username, (Information.Name != null ? Information.Name : Information.Username.Substring(0, Information.Username.IndexOf('@'))));
        }

    }
    private void MakeValid(string userName, string userData)
    {
        //////here//////

        FormsAuthentication.Initialize();
        DateTime expires = DateTime.Now.AddMinutes(21600);
        FormsAuthenticationTicket ticket =
            new FormsAuthenticationTicket(1,
                                          userName,
                                          DateTime.Now,
                                          DateTime.Now.AddMinutes(FormsAuthentication.Timeout.TotalMinutes),
                                          true,
                                          String.Empty,
                                          FormsAuthentication.FormsCookiePath);

        string encryptedTicket = FormsAuthentication.Encrypt(ticket);

        HttpCookie authCookie = new HttpCookie(
              FormsAuthentication.FormsCookieName,
              encryptedTicket);       
    }

2 个答案:

答案 0 :(得分:0)

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

将此代码放在web.config

答案 1 :(得分:0)

<sessionState mode="InProc" cookieless="true" timeout="10" />

它会将会话超时设置为10分钟。您可以相应地设置所需的超时值。如果您的会话不是无Cookie,请设置cookieless="false"