在asp.net中的登录表单不会直接到登录页面
本文关键字:登录 表单 net asp | 更新日期: 2023-09-27 18:09:11
基本上我正在使用visual studio提供的登录表单,并试图使用sql数据库进行身份验证。如果我输入不正确的数据,就会出现正确的验证,但是当我输入正确的数据时,它只是刷新当前表单,而不是注意destinationUrl部分。
我现在不知道如何解决这个问题。help
下面是我的表单代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace com.tortoise.Admin.AdminViews
{
public partial class WebForm1 : System.Web.UI.Page
{
private string strcon = WebConfigurationManager.ConnectionStrings["TortoiseDBConnectionString"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
}
private bool UserLogin(string un, string pw)
{
SqlConnection con = new SqlConnection(strcon);
SqlCommand cmd = new SqlCommand("Select Username from Admins where Username=@un and Password=@pw", con);
cmd.Parameters.AddWithValue("@un", un);
cmd.Parameters.AddWithValue("@pw", pw);
con.Open();
string result = Convert.ToString(cmd.ExecuteScalar());
if (string.IsNullOrEmpty(result)) return false; return true;
}
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
string un = Login1.UserName;
string pw = Login1.Password;
bool result = UserLogin(un, pw);
if (result)
{
e.Authenticated = true;
Session["username"] = un;
Login1.DestinationPageUrl =
String.Format("Manager.aspx?{0}", Request.QueryString.ToString());
}
else e.Authenticated = false;
}
protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
}
}
}
试试这个:
string returnUrl = Request.QueryString["ReturnUrl"];
if (returnUrl == null) returnUrl = @"~/yourfirstpage.aspx";
Response.Redirect(returnUrl);