[MVC]使用 Google reCAPTCHA
2.再來在Views/WriteMail的Index.cshtml裡面增加以下這些~
![]() |
| 最上面要引用這段 |
![]() |
| 在開始的時候要加這段 |
![]() |
| 再來放上你的key |
| 在最下面放上javescript的腳本 |
完整程式碼如下:
@{
ViewBag.Title = "Index";
}
@using reCAPTCHA.MVC
<div class="panel-heading">
我要寫信
</div>
<div class="panel-body">
@using (Html.BeginForm("Index", "WriteMail",
new { ReturnUrl = ViewBag.ReturnUrl },
FormMethod.Post, new { @class = "form-horizontal" }))
{
<div class="form-group">
<div class="col-sm-offset-3 col-sm-4 recaptchaDiv">
@Html.Recaptcha(publicKey: "你的key")
@Html.ValidationMessage("ReCaptcha")
</div>
</div>
<div class="form-group" style="text-align:center">
<button class="btn btn-default" type="submit">提交</button>
</div>
}
</div>
<script src='https://www.google.com/recaptcha/api.js' type="text/javascript"></script>
另外<div class="col-sm-offset-3 col-sm-4 recaptchaDiv">
主要是讓驗證碼可以置中及響應式大小
在Content/Site裡面加上(Site是全域CSS):
@media screen and (max-height: 575px) {
#rc-imageselect, .g-recaptcha {
transform: scale(0.77);
-webkit-transform: scale(0.77);
transform-origin: 0 0;
-webkit-transform-origin: 0 0;
}
.recaptchaDiv {
height: 60px;
}
}
3.再來到WriteMail的Controller.cs裡面加入:
| 在最上面記得引用這段 |
![]() |
| 加這段主要是因為要讓使用者一定要按驗證,否則跳出錯誤訊息 |
![]() |
| 如果你有需要讓他按了之後跳頁面或是做其他事情就加在這裏 |





沒有留言:
張貼留言