我正在创建一个带有Login控件的SignIn页面。我希望页面在经过身份验证后重定向到另一个页面。但它将我重定向到主页。(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.RRedirectFromLoginPage方法的正常行为。它重定向回最初指向登录页面的页面。查看登录页面时,您可以在ReturnUrlQueryString值中看到页面名称。
如果您想重定向到另一个页面,请尝试将switch语句的默认块更改为
default:
FormsAuthentication.SetAuthCookie(Login1.UserName, Login1.RememberMeSet);
Response.Redirect("~/DonationForm.aspx");
break;
在您的场景中,您希望使用LoggingIn事件,因为您自己验证用户,而不是依赖成员资格提供程序。
如果使用以下LoggingIn事件,则不需要ValidateUser和LoggedIn 我希望页面在经过身份验证后重定向到另一个页面。但它将我重定向到主页。(home.aspx)。奇怪。 如果希望用户重定向到某个页面,可以在web.config中设置defaultUrl。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;
}
}
}
<authentication mode="Forms" >
<forms loginUrl="~/Account/Login" defaultUrl="~/DonationForm.aspx" />
</authentication>