使用reCaptcha for asp.net library,以前一直工作正常,最近发现运行不正常了,google到了一个解决方案,不适用control而直接使用api,代码如下:
markup:
<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>
code behind:
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";
}
}