当我使用Response.Redirect()或Server.Transfer()时,我的用户会被注销

时间:2013-12-30 17:27:37

标签: c# asp.net

我在this tutorial上使用#4进行身份验证。

如果我登录,然后手动导航到我需要的页面,我就不会退出。但是,如果我使用Response.Redirect()Server.Transfer(),我的用户会立即退出。

这是我的代码

protected void  Login1_Authenticate(object sender, System.Web.UI.WebControls.AuthenticateEventArgs e)
{
    string userName = Login1.UserName;
    string password = Login1.Password;

    bool result = UserLogin(userName, password); // This method executes SQL on my own database and returns true if the username and password work
    if ((result))
    {
        e.Authenticated = true;

        // Redirect users to some page
        Response.Redirect("/home.aspx"); // THIS CAUSES A LOGOUT
    }
    else
    {
        e.Authenticated = false;
    }
}

1 个答案:

答案 0 :(得分:1)

这里的问题是Response.Redirect立即取消当前页面流中的所有剩余代码,因此通常使用Authenticated标志来指示用户已经过身份验证的代码永远不会被执行。

如果您使用的是Login控件,则可以使用控件中的DestinationPageURL属性自动重定向到Home.aspx页面。