简体   繁体   English

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

[英]Response.Redirect not working onloggedin event

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). 但是它将我重定向到主页。(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");
    }

} }

That is the normal behavior for the FormsAuthentication.RedirectFromLoginPage method you have in the switch statement. 这是switch语句中具有FormsAuthentication.RedirectFromLoginPage方法的正常行为。 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. 查看登录页面时,可以在ReturnUrl QueryString值中看到页面名称。

If you want to redirect to another page try changing the default block of the switch statement to 如果要重定向到另一个页面,请尝试将switch语句的默认块更改为

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

In your scenario, you want to use LoggingIn event, because you are validating user by yourself instead of relying on Membership Provider . 在您的方案中,您要使用LoggingIn事件,因为您是自己验证用户,而不是依赖Membership Provider

If you use the following LoggingIn event, you do not need ValidateUser and LoggedIn events. 如果使用以下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;
        }
    }
}

I want page to be redirect to another page once it got authenticated. 我希望页面通过身份验证后重定向到另一个页面。 But it redirecting me to home page.(home.aspx). 但是它将我重定向到主页。(home.aspx)。 Strange. 奇怪。

If you want a user to redirect to a page, you can set the defaultUrl in web.config. 如果希望用户重定向到页面,则可以在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