0

ASPページの要求名/パスとボタンにログインしています。

データベースへの接続文字列を作成し、次SqlDataSourceのクエリを使用して、次のようにしSqlDataSourceます。

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
     ConnectionString="<%$ ConnectionStrings:CarRentalConnectionString %>" 
     SelectCommand="SELECT * FROM [Customers] WHERE (([UserName] = @UserName) AND ([PassWord] = @PassWord))">
     <SelectParameters>
        <asp:ControlParameter ControlID="uname" Name="UserName" PropertyName="Text" Type="String" />
        <asp:ControlParameter ControlID="pass" Name="PassWord" PropertyName="Text" Type="String" />
    </SelectParameters>
</asp:SqlDataSource>

私の問題は、それをアクティブ化/実行する方法がわからず、クエリから返されたデータを取得するにはどうすればよいですか?

私はこれが間違った方向に進んでいると感じています(間違っていることを願っています)

あなたの一人が私にヒントを与えるか、正しい方向に向けてくれることを願っています:)

前もって感謝します

4

1 に答える 1

1

宣言的な方法でデータを取得するだけではないため、SqlDataSource の使用は不適切です。ユーザー検証 (検証ではない) ロジックを手続き的に定義する必要があります。

通常のアプローチは次のとおりです。パスワードはプレーンテキストで保存するのではなく、ハッシュ化する必要があることに注意してください。簡潔にするために、ハッシュソルティングは省略されています。

public static Boolean Authenticate(String userName, String password) {

    using(SqlConnection c = new SqlConnection("myConnectionString")
    using(SqlCommand cmd = c.CreateCommand()) {
        c.Open();
        cmd.CommandText = "SELECT UserName, PasswordHash, OtherDataEtc FROM Users WHERE UserName = @userName";

        cmd.Parameters.Add("@userName", SqlDbType.Varchar, 50).Value = userEnteredUserName;

        using(SqlDataReader rdr = cmd.ExecuteReader()) {
            if( !rdr.Read() ) return false; // no matching user found

            Byte[] passwordHash = rdr.GetBytes( 1 );
            Byte[] hash = Hash( userEnteredPassword ); // Use at least SHA1
            return Array.Equals( passwordHash, hash );
        }
    }
}

// Usage in ASP.NET:

public override OnPageLoad(Object sender, EventArgs e) {
    if( IsPostBack ) {
        Validate();
        if( IsValid ) {
            Boolean auth = Authenticate( this.userName, this.password ); // member textbox controls
            if( auth ) {
                FormsAuthentication.SetAuthCookie( this.userName, true );
                FormsAuthentication.RedirectFromLoginPage("somewhere", true);
            } else {
                Response.Redirect("loginFailedPage");
            }
        }
    }
}
于 2012-07-28T10:54:18.690 に答える