如何在asp.net中单击按钮时使会话到期

Asp*_*bie 1 c# asp.net session

我有一个aspx页面有2个用于用户名和密码的文本框.单击提交按钮,它将重定向到另一个页面.此页面有2个名为"过期会话"和"转到个人资料页面"的按钮.现在点击第二个按钮可以将用户带到另一个页面,在那里他可以看到该页面的内容.我想当用户点击过期会话按钮时,会话应该过期,然后当用户点击"转到个人资料页面"时按钮,他将被重定向到页面,但没有内容可见,因为会话已结束.这是提交按钮编码.

 protected void clicked(object sender, System.EventArgs e)
    {
        Session["username"] = textbox_username.Text.ToString();
        Session["password"] = textbox_password.Text.ToString();
        message.Text = "Session saved!";
        textbox_username.Text = null;
        textbox_password.Text = null;
        Response.Redirect("SessionTest.aspx");
    }
Run Code Online (Sandbox Code Playgroud)

我不知道如何在"过期会话"按钮点击会话到期.我是新手.

Ael*_*ios 7

简短问题的简答:

//Destroys the session
Session.Clear();
Session.Abandon();
Response.Redirect("Default.aspx");
Run Code Online (Sandbox Code Playgroud)

Session.Clear();在上面的代码之前使用,如果你想清理会话值,但是Session.Abandon();你会破坏它.

检查会话

if(Session["username"] == null) { }
Run Code Online (Sandbox Code Playgroud)


Agh*_*oub 5

您可以尝试使用此代码

    Session.Clear();
    Session.Abandon();
    //FormsAuthentication.SignOut(); // If you are using FormsAuthentication
    Response.Redirect("Your.aspx");
Run Code Online (Sandbox Code Playgroud)

你可以在这里查看

if(Session["username"] == null)
{
...
}
Run Code Online (Sandbox Code Playgroud)