Google reCAPTCHA - 在 IIS 中托管 - 服务器响应状态为 503(服务不可用)- MVC 5
Google reCAPTACHA - hosted in IIS - Server responded with a status of 503 (Service Unavailable) - MVC5
我使用 MVC 在我的注册页面中集成了 Google reCAPTCHA - Ajax。
我可以在我的本地主机上提交验证码。但是在我部署到服务器之后 - 出现服务不可用 503 错误。 localhost 和 demo.com
有不同的密钥
部署后我的代码没有变化。我刚刚从配置中更新了我的 Google reCAPTACHA 密钥。当表单提交操作获取服务不可用错误时。
Here is my Google reCaptacha Key
Here is my index.cshtml page code
<form id="reg-form" class="col-sm-6 col-sm-offset-3" action="@Url.Action("Submit","Registration")" method="post" novalidate>
<div class="form-group">
<label class="control-label" for="reg-name">Enter your name:</label>
<div class="regs">
<input id="FullName" name="FullName" class="form-control" type="text" data-error-empty="Please enter your name">
<i class="fa fa-user"></i>
</div>
</div><!-- End Name input -->
<div class="form-group">
<div id='recaptcha' class="g-recaptcha" data-error-empty="The captcha field is required."
data-sitekey="publicKeyfromGooglereCAPTACHA"></div>
</div>
<p>
<button name="submit" type="submit" class="btn btn-qubico btn-block g-recaptcha"
data-error-message="Fill out all information above!" data-sending-message="Sending..." data-ok-message="Message Sent"
data-callback="onSubmit">
<i class=" fa fa-location-arrow">
</i>Submit
</button>
</p> </form>
Here is my Registration.cs Controller
[HttpPost]
[System.Web.Services.WebMethod]
public JsonResult Submit(FormCollection formcollection)
{
try
{
int status = 0;
form.FullName = formcollection["FullName"];
CaptchaResponse response = ValidateCaptcha(Request["g-recaptcha-response"]);
if (response.Success)
{
status = 1;
}
else
{
status = 0;
}
return Json(status);
}
catch (Exception ex)
{
return Json(ex.Message + ex.InnerException.Message);
}
}
[HttpPost]
/// <summary>
/// Validate Captcha
/// </summary>
/// <param name="response"></param>
/// <returns></returns>
public static CaptchaResponse ValidateCaptcha(string response)
{
string secret = System.Web.Configuration.WebConfigurationManager.AppSettings["recaptchaPrivateKey"];
var client = new WebClient();
var jsonResult = client.DownloadString(string.Format("https://www.google.com/recaptcha/api/siteverify?secret={0}&response={1}", secret, response));
return JsonConvert.DeserializeObject<CaptchaResponse>(jsonResult.ToString());
}
Here is my Model CaptchaResponse.cs -
public class CaptchaResponse
{
[JsonProperty("success")]
public bool Success
{
get;
set;
}
[JsonProperty("error-codes")]
public List<string> ErrorMessage
{
get;
set;
}
}
Here is my Jquery script
$(document).ready(function () {
$('#reg-form').submit(function () {
$.ajax({
type: "POST",
url: "Registration/Submit",
data: formInput,
dataType: "json",
success: function (data) {
console.log(JSON.stringify(data));
},
error: function (data) {
console.log(JSON.stringify(data));
}
});
}
return false;
});
});
Here is my web.config code
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="reCaptchaPublicKey" value="publicKeyfromGooglereCAPTACHA" />
<add key="reCaptchaPrivateKey" value="privateKeyfromGooglereCAPTACHA" />
注意:应用程序托管在 https 端口。
如果需要更多详细信息,请告诉我。
此问题已解决。我在配置文件中添加了以下行。
<handlers>
<add name="MSCaptcha" verb="GET" path="/CaptchaImage.axd" type="MSCaptcha.captchaImageHandler, MSCaptcha"/>
</handlers>
或使用以下步骤添加处理程序
- Select 您在 IIS 中的应用程序。
- 通过双击处理程序映射在 IIS 下打开处理程序映射
或点击操作
下的 "open feature"
- 单击 "Add Managed Handler" 下的操作。
- 在类型 select 下的请求路径中输入不带引号的 "CaptchaImage.axd"
MSCaptcha.CaptchaImageHandler
- 在名称中输入不带引号的 "MSCaptcha" 然后单击确定
我使用 MVC 在我的注册页面中集成了 Google reCAPTCHA - Ajax。
我可以在我的本地主机上提交验证码。但是在我部署到服务器之后 - 出现服务不可用 503 错误。 localhost 和 demo.com
有不同的密钥部署后我的代码没有变化。我刚刚从配置中更新了我的 Google reCAPTACHA 密钥。当表单提交操作获取服务不可用错误时。
Here is my Google reCaptacha Key
Here is my index.cshtml page code
<form id="reg-form" class="col-sm-6 col-sm-offset-3" action="@Url.Action("Submit","Registration")" method="post" novalidate>
<div class="form-group">
<label class="control-label" for="reg-name">Enter your name:</label>
<div class="regs">
<input id="FullName" name="FullName" class="form-control" type="text" data-error-empty="Please enter your name">
<i class="fa fa-user"></i>
</div>
</div><!-- End Name input -->
<div class="form-group">
<div id='recaptcha' class="g-recaptcha" data-error-empty="The captcha field is required."
data-sitekey="publicKeyfromGooglereCAPTACHA"></div>
</div>
<p>
<button name="submit" type="submit" class="btn btn-qubico btn-block g-recaptcha"
data-error-message="Fill out all information above!" data-sending-message="Sending..." data-ok-message="Message Sent"
data-callback="onSubmit">
<i class=" fa fa-location-arrow">
</i>Submit
</button>
</p> </form>
Here is my Registration.cs Controller
[HttpPost]
[System.Web.Services.WebMethod]
public JsonResult Submit(FormCollection formcollection)
{
try
{
int status = 0;
form.FullName = formcollection["FullName"];
CaptchaResponse response = ValidateCaptcha(Request["g-recaptcha-response"]);
if (response.Success)
{
status = 1;
}
else
{
status = 0;
}
return Json(status);
}
catch (Exception ex)
{
return Json(ex.Message + ex.InnerException.Message);
}
}
[HttpPost]
/// <summary>
/// Validate Captcha
/// </summary>
/// <param name="response"></param>
/// <returns></returns>
public static CaptchaResponse ValidateCaptcha(string response)
{
string secret = System.Web.Configuration.WebConfigurationManager.AppSettings["recaptchaPrivateKey"];
var client = new WebClient();
var jsonResult = client.DownloadString(string.Format("https://www.google.com/recaptcha/api/siteverify?secret={0}&response={1}", secret, response));
return JsonConvert.DeserializeObject<CaptchaResponse>(jsonResult.ToString());
}
Here is my Model CaptchaResponse.cs -
public class CaptchaResponse
{
[JsonProperty("success")]
public bool Success
{
get;
set;
}
[JsonProperty("error-codes")]
public List<string> ErrorMessage
{
get;
set;
}
}
Here is my Jquery script
$(document).ready(function () {
$('#reg-form').submit(function () {
$.ajax({
type: "POST",
url: "Registration/Submit",
data: formInput,
dataType: "json",
success: function (data) {
console.log(JSON.stringify(data));
},
error: function (data) {
console.log(JSON.stringify(data));
}
});
}
return false;
});
});
Here is my web.config code
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="reCaptchaPublicKey" value="publicKeyfromGooglereCAPTACHA" />
<add key="reCaptchaPrivateKey" value="privateKeyfromGooglereCAPTACHA" />
注意:应用程序托管在 https 端口。
如果需要更多详细信息,请告诉我。
此问题已解决。我在配置文件中添加了以下行。
<handlers>
<add name="MSCaptcha" verb="GET" path="/CaptchaImage.axd" type="MSCaptcha.captchaImageHandler, MSCaptcha"/>
</handlers>
或使用以下步骤添加处理程序
- Select 您在 IIS 中的应用程序。
- 通过双击处理程序映射在 IIS 下打开处理程序映射 或点击操作 下的 "open feature"
- 单击 "Add Managed Handler" 下的操作。
- 在类型 select 下的请求路径中输入不带引号的 "CaptchaImage.axd" MSCaptcha.CaptchaImageHandler
- 在名称中输入不带引号的 "MSCaptcha" 然后单击确定