0

テキストボックスを使用して基本的なログインページを作成しました。作成したデータベースからユーザーを検証しています。以下は私のバックエンドコードです:

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">
        &nbsp;</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">
                &nbsp;<asp:Button ID="btnSubmit" runat="server" CssClass="btn1" Text="Submit" 
                    OnClick="btnSubmit_Click" Width="80px"/>
                &nbsp;&nbsp;
                <asp:Button ID="btnCancel" runat="server" CssClass="btn1" Text="Cancel" 
                    OnClick="btnCancel_Click" Width="80px"/>
            </td>
        </tr>
    </table>
</div>

今、私は次の問題を抱えています:

  1. ユーザーの種類に関係なく、ページは常に にリダイレクトされLic_Gen.aspxます。
  2. 間違ったパスワードを入力しても、ページは にリダイレクトされLic_Gen.aspxます。
  3. このページでは、入力された userType ie Adminorがその特定の andCustomerで有効であるかどうかも確認する必要があります。しかし、私もそれに到達することはできません。Login IDPassword

UserTypevalue を として明示的に定義したため、これらの問題が発生していることはわかってい0ます。

次のコードで変換しようとしたところ、 int userType = Convert.ToInt32(strnew);動作しません。

私の基本的なページを改善する方法に関するガイドラインはありますか?

4

3 に答える 3

2

あなたが書いたコードは間違っています。SQLクエリから結果が返されなくてもuserTypeに値0を与えるため、間違ったパスワードが入力されてもリダイレクトされます。また、クエリが毎回 Lic_Gen.aspx にリダイレクトされるページを作成している値を返していないと仮定しています。これらすべてのコードなしでこのすべての機能を提供する ASP メンバーシップ フレームワークを確認することをお勧めします...

于 2012-12-17T07:25:35.077 に答える
1

提供されたコードに基づいて、考えられる問題は次のとおりです。

  1. にデータが返されないsdrDatanewため、userType常に0
  2. 以下で返される DB からのデータsdrDatanew["User_Type"]は常に 0 (文字列) であるため、userType常に0

上記のポイントを使用してさらにデバッグしてください。コードに問題はありません。

注意 : コードは SQL インジェクション攻撃に対して脆弱です。詳細はhttp://en.wikipedia.org/wiki/SQL_injectionでお読みください。このサイトをどこかに公開しようと考えている方は、きっと困ることでしょう。

于 2012-12-17T07:24:47.687 に答える
0

を使用しないでください。sdrDatanew.HasRowsデータ リーダーのRead()方法のみを確認してください。

    int userType = -1;
    if (sdrDatanew.Read())
    {
        userType = Convert.ToInt32(sdrDatanew["User_Type"].ToString());
    }
于 2012-12-17T07:43:17.593 に答える