我使用文本框制作了一个基本的登录页面。我正在验证创建的数据库中的用户。下面是我的后端代码:
protected void btnSubmit_Click(object sender, EventArgs e)
{
SqlDataReader sdrDatanew = null; //Read rows one by one.
string strnew;
string connectionString = WebConfigurationManager.ConnectionStrings["Gen_LicConnectionString"].ConnectionString; //Define new connectionstring
SqlConnection connew = new SqlConnection(connectionString); //establishes a new sql connection
connew.Open(); //Opens Connection
strnew = "select User_Type from User_Details where User_Type='" + ddlUserSel.SelectedItem.Value + "' AND LoginID = '" + txtUserName.Text + "' AND Password = '" + txtPassword.Text + "'";
SqlCommand sqlCommnew = new SqlCommand(strnew, connew); //passes the command and connection
sdrDatanew = sqlCommnew.ExecuteReader(); //For select command
int userType = 0;
if (sdrDatanew.HasRows)
{
if (sdrDatanew.Read())
{
userType = Convert.ToInt32(sdrDatanew["User_Type"].ToString());
}
}
switch (userType)
{
case 0:
Response.Redirect("Lic_Gen.aspx");
break;
case 1:
Response.Redirect("Cust_Page.aspx");
break;
default:
lblDisp.Text= "Invalid User/Password";
break;
}
connew.Close();
}
这是我的前端代码:
<div>
<h2 class="style12">
LOGIN</h2>
<p class="style10">
</p>
<table align="center" cellpadding="2" cellspacing="5" style="border-width: thick;
border-style: outset; height: 195px;" class="style14">
<tr>
<td class="style16" style="border-style: none; font-family: 'Times New Roman', Times, serif;
font-size: 17.5px; font-weight: bold; font-style: normal">
User Type</td>
<td class="style17">
<asp:DropDownList ID="ddlUserSel" runat="server" Height="25px" Width="260px" CssClass="drp">
<asp:ListItem Value="0">Admin</asp:ListItem>
<asp:ListItem Value="1">Customer</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td class="style16" style="border-style: none; font-family: 'Times New Roman', Times, serif;
font-size: 17.5px; font-weight: bold; font-style: normal">
Login
</td>
<td class="style17">
<asp:TextBox ID="txtUserName" runat="server" Width="250px" CssClass="Textbox1" MaxLength="15"></asp:TextBox>
<asp:RequiredFieldValidator ID="reqUserName" runat="server" ErrorMessage="*" ForeColor="Red"
ControlToValidate="txtUserName" Font-Size="Large"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="style16" style="font-family: 'Times New Roman', Times, serif; font-size: 17.5px;
font-weight: bold; font-style: normal">
Password
</td>
<td class="style17">
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password" Width="250px" CssClass="Textbox1" MaxLength="15"></asp:TextBox>
<asp:RequiredFieldValidator ID="reqPassword" runat="server" ErrorMessage="*" ForeColor="Red"
ControlToValidate="txtPassword" Font-Size="Large"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Label ID="lblDisp" runat="server" ForeColor="Red"></asp:Label>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Button ID="btnSubmit" runat="server" CssClass="btn1" Text="Submit"
OnClick="btnSubmit_Click" Width="80px"/>
<asp:Button ID="btnCancel" runat="server" CssClass="btn1" Text="Cancel"
OnClick="btnCancel_Click" Width="80px"/>
</td>
</tr>
</table>
</div>
现在我遇到以下问题:
- 无论用户类型如何,页面总是被重定向到
Lic_Gen.aspx
. - 即使我输入了错误的密码,页面仍然会被重定向到
Lic_Gen.aspx
. - 该页面还应检查输入的 userType 是否对
Admin
特定Customer
的Login ID
and有效Password
。但我也做不到。
我知道这些问题正在到来,因为我已将UserType
value 明确定义为0
.
当我尝试使用以下代码转换它时:
int userType = Convert.ToInt32(strnew);
它不起作用。
那么关于如何改进我的基本页面的任何指导方针?