C# 如何设置/使用自定义验证程序语法
早上好 我最近正在学习C#和.net,并尝试制作注册页面 如果用户名='HelloWorld1'和密码='Password' 基本上,我试着测试,如果用户是管理员或雇员,我已经在数据库中硬编码了 然后这个测试框就会出现 然后 我怎样才能使这个代码工作C# 如何设置/使用自定义验证程序语法,c#,asp.net,C#,Asp.net,早上好 我最近正在学习C#和.net,并尝试制作注册页面 如果用户名='HelloWorld1'和密码='Password' 基本上,我试着测试,如果用户是管理员或雇员,我已经在数据库中硬编码了 然后这个测试框就会出现 然后 我怎样才能使这个代码工作 非常感谢您,祝您度过愉快的一天。嗯,是的,存在一些问题,但如果您只是想让您的示例发挥作用,请尝试以下方法。假设您的自定义验证器控件配置正确,您只想返回IsValid是true还是false int count = 0; using (SqlCon
非常感谢您,祝您度过愉快的一天。嗯,是的,存在一些问题,但如果您只是想让您的示例发挥作用,请尝试以下方法。假设您的自定义验证器控件配置正确,您只想返回IsValid是true还是false
int count = 0;
using (SqlConnection con = new System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings["FleetManagementConnectionString"].ConnectionString))
{
SqlCommand command = new SqlCommand("Select Count(*) from dbo.UserLoggins where UserName =@User and Password =@Password", con);
SqlParameter paramName = new SqlParameter("@User", SqlDbType.VarChar, 255) { Value = "HelloWorld1" };
command.Parameters.Add(paramName);
SqlParameter paramName = new SqlParameter("@Password", SqlDbType.VarChar, 255) { Value = "password" };
command.Parameters.Add(paramName);
con.Open();
count = (int)cmd.ExecuteScalar();
con.Close();
}
args.IsValid = count > 0;
return;
为什么不使用Asp.Net成员资格提供程序?实际上,您有易受SQL注入攻击的内联SQL,您的代码中有硬编码的用户名和密码,您显然将密码存储为明文,而不是散列。这是一场即将发生的安全噩梦。非常感谢!!!我将谷歌的Asp.Net会员提供商。一步一步谢谢你。我学到了很多东西。我真的很感谢你的帮助!!!!!!
protected void CustomValidator3_ServerValidate(object source, ServerValidateEventArgs args)
{
String Artists = System.Configuration.ConfigurationManager.ConnectionStrings["FleetManagementConnectionString"].ConnectionString;
System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(Artists);
SqlCommand objcmd = new SqlCommand("Select * from dbo.UserLoggins where UserName ='HelloWorld1' and Password ='password'", con);
SqlDataReader objReader;
con.Open();
objReader = objcmd.ExecuteReader();
if (objReader.HasRows)
{
args.IsValid = false;
tbody.Visible = true;
args.IsValid = true;
}
con.Close();
}
int count = 0;
using (SqlConnection con = new System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings["FleetManagementConnectionString"].ConnectionString))
{
SqlCommand command = new SqlCommand("Select Count(*) from dbo.UserLoggins where UserName =@User and Password =@Password", con);
SqlParameter paramName = new SqlParameter("@User", SqlDbType.VarChar, 255) { Value = "HelloWorld1" };
command.Parameters.Add(paramName);
SqlParameter paramName = new SqlParameter("@Password", SqlDbType.VarChar, 255) { Value = "password" };
command.Parameters.Add(paramName);
con.Open();
count = (int)cmd.ExecuteScalar();
con.Close();
}
args.IsValid = count > 0;
return;