【问题标题】:How to open make a link open in new tab?如何打开使链接在新标签页中打开?
【发布时间】:2012-08-18 17:36:03
【问题描述】:

如何使用新标签/新窗口打开 btnSMS.PostBackUrl?还是有办法转到那个 URL 然后自动转到另一个页面?

更多信息:该 URL 是一种使用服务提供商 (Clickatell) 发送 SMS 的方法。但是除了说明消息发送是否成功之外,该 URL 什么也没有。我不希望使用我的 ASP.NET 的用户卡在那里。我想让他们在发送消息后返回我的网站。

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
}

【问题讨论】:

    标签: c# asp.net webforms


    【解决方案1】:

    您的按钮应紧密反映如下:

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

    然后在代码隐藏中构建您的回发网址,然后在提交后添加以下内容:

    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 ,这将在同一浏览器中打开另一个页面。

    【讨论】:

      【解决方案2】:

      我认为实现这一点的更好方法是将表单发布到您网站中的 URL,您可以对其做出反应,然后从您的服务器发布到您正在使用的服务,并将用户重定向到相应的 URL。

      更新

      我假设你在这里使用网络表单。

      问题是您可以发布到您想要的 URL,但您的用户在页面上看不到任何更改。另一个问题是,您将服务的密码和 API_ID 放在回发 URL 中,任何可以导航到您的页面并按 F12 键的人都可以看到该 URL。公开您的 API_ID 是一个很大的错误。它应该是只有您和远程服务应该知道的事情。

      这是我的解决方案。

      当您第一次加载页面时,您将显示一个空表单并允许用户输入您想要的数据。

      然后在点击事件Handler中,获取你想要的数据并手动发布到服务中。 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 和网络负载。但我想这是您获得的安全利益的良好折衷。

      希望这会有所帮助。并随时提出任何问题。

      【讨论】:

      • 对不起,我不明白你的意思。有没有关于如何做到这一点的例子?
      【解决方案3】:

      是否可以将属性target="_blank" 添加到 btnSMS 项? 如果它是一个链接元素,它看起来像这样:

      id="btnSMS" href="" target="_blank"
      

      【讨论】:

      • 嗯,我也不太明白你的意思。抱歉,我还是个正在学习 ASP.NET 的学生。。知道如何在新标签页中打开链接吗?
      • 此提示适用于可能的 HTML 元素,它会在单击时执行您的代码。 'target="_blank"' 属性可以将链接事件重定向到新的选项卡或窗口。
      • 我假设您的代码是由 HTML 元素的事件执行的。很抱歉,如果我的回答误导了您。
      【解决方案4】:

      一个重要的切线 --- 您正在让自己面临 SQL 注入攻击的危险。

      您永远不应该、永远、永远不要将用户的文本输入与 SQL 查询连接起来。

      在 SQL 查询字符串中使用 @Parameter 值,然后使用 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

      【讨论】:

        猜你喜欢
        • 2020-07-27
        • 1970-01-01
        • 1970-01-01
        • 2011-09-29
        • 2013-11-30
        • 1970-01-01
        • 2022-01-26
        • 1970-01-01
        相关资源
        最近更新 更多