2013-06-05 92 views
4

我正在使用Recaptcha在我的網站上停止垃圾郵件。下面是的RecaptchaRecaptcha在asp.net中不工作

<asp:Panel ID="Panel1" runat="server" style="padding:5px" BackColor="White" 
      BorderColor="#999999" BorderStyle="Solid"> 
      <asp:UpdatePanel ID="UpdatePanel1" runat="server"> 
       <ContentTemplate> 
        <br /> 
        <recaptcha:RecaptchaControl ID="recaptcha" runat="server" 
         PrivateKey="XXXX-HIDDEN-XXXX" 
         PublicKey="XXXX-HIDDEN-XXXX" Theme="Red" /> 
        <br /> 
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Submit" /> 
        &nbsp; 
        <asp:Button ID="Button3" runat="server" Text="Cancel" /> 
        &nbsp;<asp:Label ID="lblResult" runat="server" Font-Size="Medium" ForeColor="Red"></asp:Label> 
       </ContentTemplate> 
      </asp:UpdatePanel> 



     </asp:Panel> 
<asp:ModalPopupExtender ID="UpdatePanel1_ModalPopupExtender" runat="server" 
        Enabled="True" PopupControlID="Panel1" TargetControlID="Button2" BackgroundCssClass="modalBackground" CancelControlID="Button3"> 
</asp:ModalPopupExtender> 

代碼的代碼Button1_Click

protected void Button1_Click(object sender, EventArgs e) 
    { 
     Page.Validate(); 
     if (Page.IsValid) 
     { 
      lblResult.Text = "All Good"; 
     } 
     else 
     { 
      lblResult.Text = "The words you entered are incorrect"; 
     } 
    } 

這段代碼的目的是,每當用戶進入東西的Recaptcha,如果字是正確的,那麼它會顯示「所有的好「,如果不是」你輸入的字不正確「。

但問題是,當我點擊Button1,其Click事件不會執行。但是,當recaptcha未被加載時(即,當互聯網未連接時),Button1_Click()執行並且僅執行在if(Page.isValid)條件之外的代碼。例如,如果我修改的單擊事件是這樣的:

protected void Button1_Click(object sender, EventArgs e) 
    { 
     lblResult.Text="Not in if condition"; 
     Page.Validate(); 

     if (Page.IsValid) 
     { 
      lblResult.Text = "All Good"; 
     } 
     else 
     { 
      lblResult.Text = "The words you entered are incorrect"; 
     } 
    } 

然後,它成功地設置lblResult.Text"Not in if condition"。但是,當加載Recaptcha時,即使是If條件以外的行也不會執行。

我試圖解決這個問題:

  1. 刪除更新面板:同樣的問題
  2. 測試實際域:同樣的問題
  3. 調試,並檢查是否有異常:無異常顯示,在輸出>調試屏幕

請告訴我有關此問題的任何其他解決方案及其發生的原因。

+0

有人請儘快給我建議儘可能... –

+0

而不是使用整個頁面的東西是啥具體有來電:我迫使它來驗證,然後檢查,如果它在後背部有效固定它你可以使谷歌API。我會建議嘗試沿着這些路線。 http://nf2p.com/dot-net/using-recaptcha-with-asp-net-and-c/ – vikingben

回答

2

我也有同樣的問題,不要使用控件,直接使用api;即:

標記:

<div> 
    <asp:Literal ID="litResult" runat="server" Mode="Encode" /> 
    <script type="text/javascript" src="http://www.google.com/recaptcha/api/challenge?k=<%= RecaptchaPublicKey %>"> 
    </script> 
    <asp:Button ID="btnSubmit" OnClick="btnSubmit_Click" Text="Submit" runat="server" /> 
</div> 

後面的代碼:

private const string RECAPTCHA_CHALLENGE_FIELD = "recaptcha_challenge_field"; 
private const string RECAPTCHA_RESPONSE_FIELD = "recaptcha_response_field"; 

protected string RecaptchaPublicKey { 
    get { return ConfigurationManager.AppSettings["RecaptchaPublicKey"]; } 
} 

protected void btnSubmit_Click(object sender, EventArgs e) { 
    var validator = new Recaptcha.RecaptchaValidator { 
    PrivateKey = ConfigurationManager.AppSettings["RecaptchaPrivateKey"], 
    RemoteIP = Request.UserHostAddress, 
    Challenge = Context.Request.Form[RECAPTCHA_CHALLENGE_FIELD], 
    Response = Context.Request.Form[RECAPTCHA_RESPONSE_FIELD]   
    }; 
    if (validator.Validate().IsValid) { 
    litResult.Text = "All Good"; 
    } else { 
    litResult.Text = "The words you entered are incorrect"; 
    } 
} 
0

沒有工作對我也 - 也許是因爲我有ValidationGroup集,也許不是。

recaptcha.Validate(); 

if (recaptcha.IsValid) { /* ...do stuff... */ }