0

新しいタブ/新しいウィンドウでbtnSMS.PostBackUrlを開く方法は?または、とにかくそのURLに移動してから、自動的に別のページに移動する方法はありますか?

詳細:そのURLは、サービスプロバイダー(Clickatell)を使用してSMSを送信する方法です。しかし、URLには、メッセージの送信が成功したかどうかを示す以外に何もありませんでした。ASP.NETを使用しているユーザーがそこにとどまってほしくありません。メッセージを送信した後、彼らが私のWebサイトに戻ることを許可したいと思います。

protected void btnSMS_Click1(object sender, EventArgs e)
{

    String HP = txtHP.Text;
    String URL = "http://api.clickatell.com/http/sendmsg?user=myuser&password=mypassword&api_id=myapi_id&to=";
    String Text = "&text=" + txtSMS.Text;
    btnSMS.PostBackUrl = URL + HP + Text;

    string username;
    //Sql Connection to access the database
    SqlConnection conn6 = new SqlConnection("MY CONNECTION");
    //Opening the Connnection
    conn6.Open();
    string mySQL;
    username = HttpContext.Current.User.Identity.Name;
    //Insert the information into the database table Login
    mySQL = "INSERT INTO Table_Message(Username, Message, Title, SendTo) VALUES('" + username + "','" + txtSMS.Text.Trim() + "','" + txtTitle.Text.Trim() + "','" + txtHP.Text.Trim() + "')";

    SqlCommand cmdAdd = new SqlCommand(mySQL, conn6);
    //Execute the sql command
    cmdAdd.ExecuteNonQuery();
    //Close the Connection
}
4

4 に答える 4

1

ボタンは次のように厳密に反映されます。

<asp:Button runat="server" ID="btnSMS"  UseSubmitBehavior="false"  OnClick="btnSMS_Click1"  Text="Send/> 

次に、分離コードでポストバック URL を作成し、送信後に次を追加します。

protected void btnSMS_Click1(object sender, EventArgs e)
        {


 String HP = txtHP.Text;
            String URL = "http://api.clickatell.com/http/sendmsg?user=myuser&password=mypassword&api_id=myapi_id&to=";
            String Text = "&text=" + txtSMS.Text;
            string UrlFinal = URL + HP + Text;

            string username;
            //Sql Connection to access the database      
            SqlConnection conn6 = new SqlConnection("Data Source=19-17\\sqlexpress;" + "Initial Catalog = Suite2; Integrated Security =SSPI");
            //Opening the Connnection      
            conn6.Open();
            string mySQL;
            username = HttpContext.Current.User.Identity.Name;
            //Insert the information into the database table Login      
            mySQL = "INSERT INTO Table_Message(Username, Message, Title, Startdate, Enddate, SendTo) VALUES('" + username + "','" + txtSMS.Text.Trim() + "','" + txtTitle.Text.Trim() + "','" + txtHP.Text.Trim() + "')";

            SqlCommand cmdAdd = new SqlCommand(mySQL, conn6);
            //Execute the sql command      
            cmdAdd.ExecuteNonQuery();
            //Close the Connection 

            this.ClientScript.RegisterStartupScript(this.GetType(),
                                        "navigate",
                                        "window.open('" + UrlFinal + "');",
                                        true); 

        }

これは、新しいウィンドウで開く必要があります。別の方法として、同じブラウザで別のページを開く window.navigate を使用することもできます。

于 2012-08-15T10:49:25.870 に答える
1

これを実装するためのより良い方法は、フォームをサイト内の URL に投稿して反応できるようにし、サーバーから使用しているサービスに投稿して、ユーザーを適切な URL にリダイレクトすることだと思います。 .

アップデート

ここで Web フォームを使用していると仮定します。

問題は、必要な URL に投稿できることですが、ユーザーはページに何の変化も見ません。もう 1 つの問題は、サービスのパスワードと API_ID をポストバック URL に入れていることです。これは、ページに移動して F12 キーを押すことができる人なら誰でも見ることができます。API_ID を公開するのは大きな間違いです。これは、あなたとリモート サービスだけが知っているはずの情報です。

これが私の解決策です。

ページを初めて読み込むと、空のフォームが表示され、ユーザーが必要なデータを入力できるようになります。

次に、クリック イベント ハンドラーで、必要なデータを取得し、サービスに手動で投稿します。HTTP ポストには、サービスに送信するキーと値のペアのリストが含まれています。ポスト リクエストを手動で作成し、リモート サービスからレスポンスを取得する必要があります。

クリック ハンドラは次のようになります。

protected void btnSMS_Click1(object sender, EventArgs e)
 {
    Dictionary<string,string> postValues = new   Dictionary<string,string>();

    // gather the key value pairs ou want from your controls and add them to the dictionary.
   // and call postSynchronous method (which I'll explain below)
   string result = postSynchronous(URLtoPOST,postValues);

   if (result= yourSuccessValue){
       // redirect to a page that says.. 'Yay!! you successfully posted to the service ?'
       Server.Transfer("successPage.aspx");
   }else
   {
      // what to do if it fails ?
   }
}

ここで、postSynchronousメソッド (または呼び出したいもの) を手動で作成する必要があります。ポストする RUL と、ポストと一緒に送信するキーと値のペアのリストを取得し、実際のポスト リクエストを作成します。

これを行う方法を学ぶためのチュートリアルについては、このリンクをご覧ください。 http://petewarden.typepad.com/searchbrowser/2008/09/how-to-post-an.html

メソッドと関連メソッドのコードを埋め込もうとしましたが、長すぎました。

したがって、この方法で物事を行う場合、API キーやパスワードをユーザーのブラウザーに送信することがないため、ユーザーに知られることはありません。(そうしないと、キーを使用してサービスにアクセスできますが、これはセキュリティホールです)

これで悪いのは、ソリューションと比較して、サーバーに投稿しているため、サーバーにその CPU とネットワークの負荷がかかることです。しかし、得られるセキュリティ上の利点とのトレードオフは良いと思います。

お役に立てれば。ご不明な点がございましたら、お気軽にお問い合わせください。

于 2012-08-15T09:09:25.363 に答える
0

重要な点 --- SQL インジェクション攻撃に対して危険なほど無防備なままになっています。

ユーザーからのテキスト入力を SQL クエリと連結してはなりません。

@ParameterSQL クエリ文字列の値を使用してから、パラメーターを使用SqlCommandしてそれらの値を置き換えます。

string mySQL = "INSERT INTO Table_Message(Username, Message, Title, SendTo) VALUES(@Username, @Message, @Title, @SendTo)";
SqlCommand cmdAdd = new SqlCommand(mySQL, conn6);
cmdAdd.Parameters.AddWithValue("@Username", username);
cmdAdd.Parameters.AddWithValue("@Message", txtSMS.Text.Trim());
cmdAdd.Parameters.AddWithValue("@Title", txtTitle.Text.Trim());
cmdAdd.Parameters.AddWithValue("@SendTo", txtHP.Text.Trim());
cmdAdd.ExecuteNonQuery();

タイプを明示的に指定する方が良いでしょう:

cmdAdd.Parameters.Add("@Username", SqlDbType.NVarChar, 100).Value = username;

Troy Hunt の素晴らしいシリーズをご覧ください - http://www.troyhunt.com/2010/05/owasp-top-10-for-net-developers-part-1.html

于 2012-08-18T09:36:03.263 に答える
0

プロパティtarget="_blank"を btnSMS アイテムに追加することはできますか? リンク要素の場合、次のようになります。

id="btnSMS" href="" target="_blank"
于 2012-08-15T09:16:07.303 に答える