Response.Redirect不工作onloggedin事件
问题描述:
我正在用Login控件创建一个SignIn页面。我希望页面在被认证后重定向到另一个页面。但它将我重定向到主页。(home.aspx)。奇怪。请指教。Response.Redirect不工作onloggedin事件
<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事件,因为你自己验证用户,而不是依靠会员供应商。
如果您使用以下LoggingIn事件,你不需要的ValidateUser和的loggedIn事件。
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>
的 –
user3115435
2014-09-03 21:02:33
您是否使用表单身份验证?你能粘贴web.config的代码部分吗? – Partha 2014-09-03 21:24:44
它开始为我工作。我更改了defaulturl =“〜DonationForm.aspx” authentication> –
user3115435
2014-09-03 21:38:11