0

私のリンクは

http://localhost/default.aspx?phone=9057897874&order=124556

これは、ASP.net から URL にパラメーターを渡すための私の基本的なページです。

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"     Inherits="WebApplication2._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form method="get" action="default.aspx">

<label>Phone No</label>
<input type="text" name="phone" />
<br />
<label>Order No</label>
<input type="text" name="order" />
<br />
<input type="submit" value="submit" />
<br />
</form>

パラメータを変数に保存できる私のC#ファイル

namespace WebApplication2
 {
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string strQuery;

        string phone = Request.QueryString["phone"];
        if (phone != null)
        {
            Response.Write("phone no is ");
            Response.Write(phone);
        }
        else

        {
            Response.Write("You phone number is not correct");
        }

        string order_no = Request.QueryString["order"];
        if (order_no != null)
        {
            Response.Write("Order No is ");
            Response.Write(order_no);
        }
        else
        {
            Response.Write("You Order number is not correct");
        }

//How I can Connect to Mysql Server

        strQuery = "SELECT order_status where orde01=''" + order_no + "'' and phone01=''" + phone + "''";

        Response.Write(strQuery);
}
}

私はこのようなことをしようとしていますが、クエリ全体を文字列としてしか提供しません。私はこのトピックについて初めてです。どんな助けでも感謝しますありがとう

4

2 に答える 2

2

まず、ユーザーが変更できる入力に基づいて SQL ステートメントを連結すること、特に文字列として格納されている場合は、SQL インジェクションの脆弱性が作成される方法です。その男にならないでください。

クエリ文字列をトークン化するには、名前付きパラメーターを使用します。これがクエリ文字列であると仮定します

?orderid=777&phone=777-777-7777

Response.QueryString["orderid"] 

「777」を返し、

Response.QueryString["phone"] 

'777-777-7777' を返します

SQL インジェクションの問題に関しては、いくつかのオプションがあります。1 つはパラメーター化された sql ステートメントです。ここで C# の例を参照してください: http://rosettacode.org/wiki/Parametrized_SQL_statement またはパラメーターを持つストアド プロシージャを使用します。最も望ましくないが最小限に許容されるオプションは、入力パラメーターを厳密に正規表現で検証することです。特に、「=;%」などの文字を削除します。

編集:サンプルを作成する時間ができたので、これをチェックしてください。このサンプルはデータベースに合わせてカスタマイズする必要がありますが、テスト テーブルを使用して私の mysql DB で動作しています。コードが正しくコンパイルされる前に、 MySQLConnectorパックをインストールし、'MySql.Data' へのプロジェクト参照を追加する必要があります。

namespace WebApplication2
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e) {
            //define some regex patterns for validating our data.
            const string PHONEREGEX = @"((\(\d{3}\))|(\d{3}-))\d{3}-\d{4}";
            const string ORDERNUMREGEX = @"\d*";

            bool isValid = true;

            string phone = Request.QueryString["phone"]; //read phone from querystring.

            //validate that arg was provided, and matches our regular expression. this means it contains only numbers and single hyphens
            if(!string.IsNullOrWhiteSpace(phone) && System.Text.RegularExpressions.Regex.IsMatch(phone, PHONEREGEX)){
                Response.Write(HttpUtility.HtmlEncode(string.Format("The phone number is {0}", phone))); //HTML Encode the value before output, to prevent any toxic markup.
            } else {
                Response.Write("Phone number not provided.");
                isValid = false;
            }

            string orderStr = Request.QueryString["order"]; //read ordernum from querystring
            long order = long.MinValue;

            //validate that order was provided and matches the regex meaning it is only numbers. then it parses the value into 'long order'.
            if(!string.IsNullOrWhiteSpace(orderStr) && System.Text.RegularExpressions.Regex.IsMatch(orderStr, ORDERNUMREGEX) && long.TryParse(orderStr, out order)){
                Response.Write(HttpUtility.HtmlEncode(string.Format("The order number is {0}", order))); //use 'long order' instead of orderStr.
            } else {
                Response.Write("Order number not provided.");
                isValid = false;
            }

            //if all arguments are valid, query the DB.
            if (isValid) {
                Response.Write(GetOrderStatus( phone, order));
            }

        }

        private static string GetOrderStatus(string phone, long order) {
            string status = "";

            //create a connection object
            string connstring = "SERVER=<YOUR MYSQL SERVER>;DATABASE=<YOUR DATABASE>;UID=<YOUR USER>;PASSWORD=<YOUR PASSWORD>-";//this is a connection string for mysql. customize it to your needs.
            MySql.Data.MySqlClient.MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection(connstring); //put your connection string in this constructor call 

            //create a SQL command object
            using (MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand()) { //use a using clause so resources are always released when done.
                cmd.Connection = conn;
                cmd.CommandText = "SELECT `Order_Status` FROM `<YOUR TABLE>` WHERE `Order` = @order AND `Phone` = @phone"; //this needs a From statement 

                //add parameters for your command. they fill in the @order and @phone in the sql statement above. customize these to match the data types in your database.
                cmd.Parameters.Add("order", MySql.Data.MySqlClient.MySqlDbType.Int64,11).Value = order; //do not use @ sign in parameter name
                cmd.Parameters.Add("phone", MySql.Data.MySqlClient.MySqlDbType.VarChar, 50).Value = phone;

                //execute the command, read the results from the query.
                cmd.Connection.Open();
                using (MySql.Data.MySqlClient.MySqlDataReader reader = cmd.ExecuteReader()) {
                    while (reader.Read()) {
                        status = reader.GetString("Order_Status");
                    }
                    cmd.Connection.Close();
                }

            }
            return status;
        }
    }
}
于 2012-11-08T17:33:36.730 に答える
0

あなたが使用している必要があります

Request.Form["phone"]
Request.Form["order"]

それ以外の

Request.QueryString["phone"]
Request.QueryString["order"]

この理由は、ポストバックを行っており、それらの値がクエリ文字列として設定された URL に決してリダイレクトしないためです。

ただし、質問のタイトルは、次のようなものを含む URL があることを示唆しています。

http://yourdomain.com?phone=0123456789&order=17

于 2012-11-08T17:27:08.120 に答える