简体   繁体   English

会话超时-如何正确地“记住我”?

[英]Session timeout - how to make “remember me” properly?

The code (in global.asax): 代码(在global.asax中):

void Session_Start(object sender, EventArgs e)
{
    // remember me
    HttpCookie rememberCookie = HttpContext.Current.Request.Cookies["remember"];
    if (rememberCookie != null)
    {
        if (rememberCookie.Value == "true")
            HttpContext.Current.Session.Timeout = 464800;
        else
            HttpContext.Current.Session.Timeout = 70;
    }
    else
        HttpContext.Current.Session.Timeout = 60;
}

Basicly, i want to make "remember me" using session and cookie. 基本上,我想使用会话和cookie来“记住我”。 This is the code, when login in the home.aspx.cs 这是登录home.aspx.cs时的代码

public void _loginFrmSubmit(object sender, EventArgs e)
{
    Session["User"] = home_username.Text;
    HttpCookie rememberCookie;
    //rememberCookie.HttpOnly = true;
    if (remember.Checked)
    {
        rememberCookie = new HttpCookie("remember", "true");
        rememberCookie.Expires = DateTime.Now.AddMonths(12);
    }
    else
    {
        rememberCookie = new HttpCookie("remember", "false");
        rememberCookie.Expires = DateTime.Now.AddDays(1);
    }
    HttpContext.Current.Response.Cookies.Add(rememberCookie);
    Response.Redirect(Request.RawUrl);
}

The code working fine when first fired. 该代码在首次触发时工作正常。 But after I log out: 但是我注销后:

public void _Quit(object sender, EventArgs e)
{
    HttpCookie rememberCookie = new HttpCookie("remember");
    rememberCookie.Expires = DateTime.Now.AddDays(-1);
    Response.Cookies.Add(rememberCookie);
    Session.Clear();
    Response.Redirect(Request.RawUrl);
}

And I login again. 然后我再次登录。 The session.timeout is not changing. session.timeout不变。 It get stuck with the first timeout set. 它卡在第一个超时设置中。 Please help me make that everytime i logout, and login, the session timeout changes. 请帮助我确保每次注销和登录时,会话超时都会更改。

Ok, this was pretty stupid of me to do all that stuff, because i didn't have to add any cookies and work with session start at all. 好的,我做所有这些事情真是愚蠢,因为我不必添加任何cookie并完全可以使用session start。 All i have to do is to set the session timeout in the aspx.cs (login submit) itself. 我要做的就是在aspx.cs(登录提交)本身中设置会话超时。

public void _loginFrmSubmit(object sender, EventArgs e)
{
            Session["User"] = home_username.Text;
            if (remember.Checked)
                Session.Timeout = 464800;
            else
                Session.Timeout = 120;
            Response.Redirect(Request.RawUrl);
            Response.Redirect(Request.RawUrl);
}

I just didn't know i can do that, because it didn't worked for me the last time i remember i tried it. 我只是不知道我能做得到,因为上次我记得我尝试过它对我没有用。 It is strange that there is no examples and tutorials of using sessions like that. 奇怪的是,没有使用此类会话的示例和教程。 I didn't found anything, any form in the internet that would show an example of using sessions with 'remember me' option. 我没有发现任何东西,互联网上的任何形式都显示了使用带有“记住我”选项的会话的示例。 I don't know why no one answered my question, it is that simple. 我不知道为什么没人回答我的问题,就是这么简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM