1

以下にこの簡単なログインページがあります。

正しいID + pwを入力した場合->成功(これが必要です)

間違ったIDを入力した場合->間違ったログイン(これが必要です)

しかし、正しい ID + 間違った ID を入力すると、間違ったパスワードを言ってほしいです。

どうすればいいですか?

ありがとうございました。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["X"] != null)
        {
            Response.Redirect("MemberPage.aspx");
        }
    }

    SqlConnection cnn = new SqlConnection("Initial Catalog=Northwind;Data Source=localhost;Integrated Security=SSPI;");

    protected void Button1_Click(object sender, EventArgs e)
    {

        cnn.Open();
        SqlCommand cmd = new SqlCommand("SELECT FirstName,LastName FROM Employees", cnn);
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.HasRows)
        {
            while (dr.Read())
            {
                if (TextBox1.Text == dr.GetString(0) || TextBox2.Text == dr.GetString(1))
                    {
                            Session["x"] = TextBox1.Text;
                            Response.Redirect("MemberPage.aspx");
                    }
                else
                {
                    Label2.Text = "wrong login";
                }
            }
        }

        cnn.Close();

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("Register.aspx");
    }
}
4

3 に答える 3

2

これはあなたの質問への回答ではありませんが、あなたのロジックには重大なセキュリティ上の欠陥があることがわかります。ユーザーがどのような失敗、無効なユーザー名、無効なパスワードに遭遇しても、常に同じ「無効なログイン」メッセージを表示する必要があると思います。

システムに侵入しようとしている人がいる場合、ユーザー アカウントが存在すること (無効なパスワード) を確認すると、ブルート フォースを使用してその特定のアカウントのパスワードをクラックし始める可能性があります。

ちょうど考えるべきこと。

于 2013-10-05T14:33:14.137 に答える
0

ここでロジックを間違って配置しています。ロジックは

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["X"] != null)
        {
            Response.Redirect("MemberPage.aspx");
        }
    }

    SqlConnection cnn = new SqlConnection("Initial Catalog=Northwind;Data Source=localhost;Integrated Security=SSPI;");

    protected void Button1_Click(object sender, EventArgs e)
    {

        cnn.Open();
        SqlCommand cmd = new SqlCommand("SELECT FirstName,LastName FROM Employees", cnn);
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.HasRows)
        {

                if (TextBox1.Text.Trim() == dr.GetString(0) || TextBox2.Text.Trim()== dr.GetString(1))
                    {
                        if (TextBox2.Text.Trim()== dr.GetString(1))
                        {
                            Session["x"] = TextBox1.Text.Trim();
                            Response.Redirect("MemberPage.aspx");
                        }
                        else
                        {
                            Label2.Text = "wrong password";
                        }
                    }
                else
                {
                    Label2.Text = "wrong login";
                }

        }

        cnn.Close();

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("Register.aspx");
    }
}
于 2013-10-05T14:30:12.917 に答える
0

データベースから名と姓を読み取りますが、姓に対してパスワードを確認します。このフィールドに有効なパスワードが含まれているとは思えません

この論理エラーの一部として、ステートメントで WHERE 句を使用して、ユーザーがデータベースに存在するかどうかを確認する必要があります。

protected void Button1_Click(object sender, EventArgs e)
{
    // Command with parameters that check if a user with the supplied credentials exists
    // If the user exists then just one record is returned from the datatable....
    string cmdText = "SELECT FirstName,LastName " + 
                     "FROM Employees " + 
                     "WHERE username=@uname and pass=@pwd";
    using(SqlConnection cnn = new SqlConnection(.....))
    using(SqlCommand cmd = new SqlCommand(cmdText, cnn))
    {
         cnn.Open();
         cmd.Parameters.AddWithValue("@uname", TextBox1.Text);
         cmd.Parameters.AddWithValue("@pwd", TextBox2.Text);
         using(SqlDataReader reader = cmd.ExecuteReader())
         {
              // If the Read returns true then a user with the supplied credentials exists 
              // Only one record is returned, not the whole table and you don't need to 
              // compare every record against the text in the input boxes 
              if(reader.Read())
              {
                   Session["x"] = reader.GetString(0);
                   Response.Redirect("MemberPage.aspx");
              }
              else
              {
                   Label2.Text = "Invalid credentials";
              }
         }
     }
 }

もう 1 つの注意点は次のとおりです。データベースでは、平文のパスワードを使用しないでください。パスワードを保存する正しい方法は、パスワードに対応するハッシュ化された文字列を保存し、ハッシュ関数をユーザー入力に適用して、データベース内の同じハッシュ化された文字列をチェックすることです。

于 2013-10-05T14:33:40.047 に答える