テキストボックスを使用して基本的なログインページを作成しました。作成したデータベースからユーザーを検証しています。以下は私のバックエンドコードです:
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 ie
Admin
orがその特定の andCustomer
で有効であるかどうかも確認する必要があります。しかし、私もそれに到達することはできません。Login ID
Password
UserType
value を として明示的に定義したため、これらの問題が発生していることはわかってい0
ます。
次のコードで変換しようとしたところ、
int userType = Convert.ToInt32(strnew);
動作しません。
私の基本的なページを改善する方法に関するガイドラインはありますか?