繁体   English   中英

Response.Redirect在onloggedin事件中不起作用

[英]Response.Redirect not working onloggedin event

我正在使用登录控件创建登录页面。 我希望页面通过身份验证后重定向到另一个页面。 但是它将我重定向到主页。(home.aspx)。 奇怪。 请指教。

 <asp:Login ID="Login1" runat="server" OnAuthenticate="ValidateUser" OnLoggedIn="Login1_LoggedIn" DestinationPageUrl="~/DonationForm.aspx">
    </asp:Login>

public partial class Login : System.Web.UI.Page
{

    protected void ValidateUser(object sender, EventArgs e)
    {
        int userId = 0;
        string constr = ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand("Validate_User"))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@Username", Login1.UserName);
                cmd.Parameters.AddWithValue("@Password", Login1.Password);
                cmd.Connection = con;
                con.Open();
                userId = Convert.ToInt32(cmd.ExecuteScalar());
                con.Close();
            }
            switch (userId)
            {
                case -1:
                    Login1.FailureText = "Username and/or password is incorrect.";
                    break;
                case -2:
                    Login1.FailureText = "Account has not been activated.";
                    break;
                default:
                    FormsAuthentication.RedirectFromLoginPage(Login1.UserName, Login1.RememberMeSet);
                    break;
            }
        }
    }



    protected void Login1_LoggedIn(object sender, EventArgs e)
    {
        Response.Redirect("~/DonationForm.aspx");
    }

}

这是switch语句中具有FormsAuthentication.RedirectFromLoginPage方法的正常行为。 它重定向回到最初指向登录页面的页面。 查看登录页面时,可以在ReturnUrl QueryString值中看到页面名称。

如果要重定向到另一个页面,请尝试将switch语句的默认块更改为

default:
    FormsAuthentication.SetAuthCookie(Login1.UserName, Login1.RememberMeSet);
    Response.Redirect("~/DonationForm.aspx"); 
    break;

在您的方案中,您要使用LoggingIn事件,因为您是自己验证用户,而不是依赖Membership Provider

如果使用以下LoggingIn事件,则不需要ValidateUserLoggedIn事件。

protected void Login1_LoggingIn(object sender, LoginCancelEventArgs e)
{
    int userId = 0;
    string constr = ConfigurationManager.ConnectionStrings
        ["DatabaseConnectionString"].ConnectionString;

    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand("Validate_User"))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Username", Login1.UserName);
            cmd.Parameters.AddWithValue("@Password", Login1.Password);
            cmd.Connection = con;
            con.Open();
            userId = Convert.ToInt32(cmd.ExecuteScalar());
            con.Close();
        }
        switch (userId)
        {
            case -1:
                Login1.FailureText = "Username and/or password is incorrect.";
                break;
            case -2:
                Login1.FailureText = "Account has not been activated.";
                break;
            default:
                // RedirectFromLoginPage will take care of creating 
                // authentication cookie and redirect; you do not need 
                // to do anything.
                FormsAuthentication.RedirectFromLoginPage(
                    Login1.UserName, Login1.RememberMeSet);
                break;
        }
    }
}

我希望页面通过身份验证后重定向到另一个页面。 但是它将我重定向到主页。(home.aspx)。 奇怪。

如果希望用户重定向到页面,则可以在web.config中设置defaultUrl

<authentication mode="Forms" >
  <forms loginUrl="~/Account/Login" defaultUrl="~/DonationForm.aspx" />
</authentication>

暂无
暂无

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

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