protected void btnlogin_Click(object sender, EventArgs e)
{
string name = txtname.ToString().Trim();
string pass = txtpassword.ToString().Trim();
using (SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=StaffMs;Integrated Security=True"))
{
SqlCommand com = new SqlCommand("select * from dbo.admin where ad_name=@a and password=@b",con);
com.Parameters.Add(new SqlParameter("@a",name));
com.Parameters.Add(new SqlParameter("@b",pass));
con.Open();
SqlDataReader dr=com.ExecuteReader();
if(dr.Read())
{
Response.Redirect("Default.aspx");
}
else
{
// Response.Write("用户名或密码输入错误");
Response.Write("<script>alert('用户名或密码输入错误')</script>");
}
con.Close();
}
}用户名和密码都是对的,但是就是提示“用户名或密码输入错误”,大家帮忙看看啊!
解决方案 »
- webconfig中配置身份验证
- [求助]无法创建抽象类或接口“System.Collections.Generic.IList<Test>”的实例
- 新闻页面中当没有该新闻的图片时,如何在该页面的.vb文件中让IMAGE控件的可视属性为FALSE?(代码很少,就几行)
- TextBox数据绑定问题,救命
- 如何动态生成master/detail表记录?
- 项目中怎样新建一个.inc文件
- 用AJAX时,用到了UpdatePanel,这个控件如何排版
- DataGrid 如何在修改数据后,点更新的时候验证数据的有效性?
- 有关iform的问题?求解
- chart用jquery打印的时候出现的是一个叉叉
- gridview的allowsorting设置只针对自动生成列吗?绑定列不可以用?
- 在.NET中让表格合并,拆分?
{
Response.Redirect("Default.aspx");}
else{
Response.Write("<script>alert('用户名或密码输入错误')</script>");}
有个hasrow把,你代码写的……
//
// Summary:
// Verifies that the specified user name and password exist in the data source.
//
// Parameters:
// username:
// The name of the user to validate.
//
// password:
// The password for the specified user.
//
// Returns:
// true if the specified username and password are valid; otherwise, false.
public abstract bool ValidateUser(string username, string password);
SqlDataReader dr=com.ExecuteReader();
参数化查询 '(@a nvarchar(4000),@b nvarchar(33))select * from dbo.admin where' 需要参数 @a,但未提供该参数。
看不懂哪里出错了!
SqlDataReader dr=com.ExecuteReader();
if(dr.HasRows)
{
if (dr.ToString() != "")
{
Response.Redirect("Default.aspx");
}
else
{
}
}
else
{
// Response.Write("用户名或密码输入错误");
Response.Write("<script>alert('用户名或密码输入错误')</script>");
}
这样写SQL 会被注入
protected void btnlogin_Click(object sender, EventArgs e)
{
string name = txtname.ToString().Trim();
string pass = txtpassword.ToString().Trim();
using (SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=StaffMs;Integrated Security=True"))
{
SqlCommand com = new SqlCommand("select count(*) from dbo.admin where ad_name=@a and password=@b",con);
com.Parameters.Add(new SqlParameter("@a",name));
com.Parameters.Add(new SqlParameter("@b",pass));
con.Open();
int n = (int)Comm.ExecuteScalar();
if(n>0) {
Response.Redirect("Default.aspx");
}
else
{
// Response.Write("用户名或密码输入错误");
Response.Write("<script>alert('用户名或密码输入错误')</script>");
}
con.Close();
}
}
用户代码为处理
未将对象引用设置到对象的实例。
...... com.Parameters.Add(new SqlParameter("@a", SqlType.VarChar,20));
com.Parameters["@a"].Value = name;
com.Parameters.Add(new SqlParameter("@b", SqlType.VarChar, 20));
com.Parameters["@b"].Value = pass;
........
2、将你的sql输出出来,放到查询分析器里看看,是否有数据。
用户可以这样写 select * from dbo.admin where ad_name=haha and password=12154 and 1=1
改成Executenonquery()看看
{
string name = txtname.ToString().Trim();
string pass = txtpassword.ToString().Trim();改成
protected void btnlogin_Click(object sender, EventArgs e)
{
string name = txtname.TextTrim();
string pass = txtpassword.Text.Trim();
解决了!这样运行就好了!呵呵。谢谢大家的关注啊!