Response.Redirect的不工作onloggedin事件 [英] Response.Redirect not working onloggedin event

查看:182
本文介绍了Response.Redirect的不工作onloggedin事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创造与登录控件的登录页面。我想页面被重定向到另一个页面,一旦它得到了验证。但它重定向我的主页。(home.aspx)。奇怪。请指教。

I am creating a SignIn page with Login control. I want page to be redirect to another page once it got authenticated. But it redirecting me to home page.(home.aspx). Strange. Please advise.

 <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");
    }

}

推荐答案

这是在 FormsAuthentication.RedirectFromLoginPage 方法,你在switch语句中有正常的行为。它重定向回最初定向到登录页面的页面。您可以看到页面名称在 RETURNURL 查询字符串值,同时查看登录页面。

That is the normal behavior for the FormsAuthentication.RedirectFromLoginPage method you have in the switch statement. It redirects back to the page that originally directed to the login page. You can see the page name in the ReturnUrl QueryString value while viewing the login page.

如果你想重定向到另一页尝试改变switch语句的默认块

If you want to redirect to another page try changing the default block of the switch statement to

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

这篇关于Response.Redirect的不工作onloggedin事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆