【发布时间】:2012-11-23 02:23:12
【问题描述】:
我基本上是在使用 VS2008 和 SQL Server 2005 创建一个网站,它以登录页面启动。现在我要验证用户输入的LoginID 和Password。一旦系统从数据库表中找到 ID 和密码,就会进行此身份验证。找到后,我想检查它是哪种用户,即Admin 或Customer。如果用户是管理员,则页面应重定向到abc.aspx,否则为cde.aspx。
我的 LoginPage 前端是:
<tr>
<td class="style11"> Login </td>
<td>
<asp:TextBox ID="txtUserName" runat="server" Width="300px" CssClass="Textbox1"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style11"> Password </td>
<td>
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password" Width="300px" CssClass="Textbox1"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" CssClass="btn1"
Text="Submit" />
<asp:Button ID="btnCancel" runat="server" OnClick="btnCancel_Click" CssClass="btn1"
Text="Cancel" />
</td>
</tr>
而我的后端代码是:
//CODE 1
SqlDataSource sds = new SqlDataSource();
sds.ConnectionString = ConfigurationManager.ConnectionStrings["Gen_LicConnectionString3"].ToString();
sds.SelectParameters.Add("LoginID", TypeCode.String, this.txtUserName.Text);
sds.SelectParameters.Add("Password", TypeCode.String, this.txtPassword.Text);
sds.SelectCommand = "SELECT User_Type FROM [User_Details] WHERE [LoginID]=@LoginID AND [Password]=@Password";
if (//Some Condition) //<-- Here I want to check the condition whether the User_Type is 'Admin' or 'Customer'
{
Response.Redirect("Lic_Gen.aspx"); //<-- If Admin
}
else
{
Response.Redirect("Cust_Page.aspx"); //<-- If Customer
}
//CODE 2
//string connectionString = WebConfigurationManager.ConnectionStrings["Gen_LicConnectionString3"].ConnectionString;
//string selectSQL = "SELECT User_Type FROM User_Details WHERE [LoginID]=@LoginID AND [Password] = @Password";
//SqlConnection con = new SqlConnection(connectionString);
//SqlCommand cmd = new SqlCommand(selectSQL, con);
//SqlDataAdapter adapter = new SqlDataAdapter(cmd);
//DataSet ds = new DataSet();
//if (cmd.Equals(1))
//{
// Response.Redirect("Lic_Gen.aspx");
//}
//else
//{
// Response.Redirect("Cust_Page.aspx");
//}
【问题讨论】:
-
无论用户类型是什么——无论是管理员还是客户——页面都会被重定向到
Cust_Page -
您看过 ASP.Net 会员模型吗?它几乎是开箱即用的......另外,请 - 请 - 不要以纯文本形式存储密码。您应该使用安全的散列算法和盐值对它们进行散列。
-
这也会导致Sql注入。
-
if (dv.Count == 0)如何告诉您用户是管理员? -
@NevilleK :当然,在涉及密码时我会牢记这一点。关于会员模式。我会检查一下。
标签: c# asp.net database sql-server-2005 search