我遇到了一个错误:
System.InvalidOperationException:公钥不能为空。
我的控制器页面 Employees/Create 如下:
@using System.Collections
@using System.Collections.Concurrent
@using System.Data.SqlTypes
@using DataAccess
@using Recaptcha.Web.Mvc
@using Recaptcha.Web;
@using Recaptcha.Web.Mvc;
@model DataAccess.Employee
@{
ViewBag.Title = "Create";
}
@{ Layout = "/views/shared/_EmployeeLayout.cshtml";
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="container">
<h2>Please, provide some information.</h2>
<div class="row">
<div class="col-md-8">
<div class="form-group">
@Html.LabelFor(model => model.Name, "Name", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
<br />
<br />
<div class="form-group">
@Html.LabelFor(model => model.Surname, "Surname", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Surname, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Surname, "", new { @class = "text-danger" })
</div>
</div>
<br />
<br />
<div class="form-group">
@Html.LabelFor(model => model.Location, "Location", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("LocationID", null, "--Please pick one--", new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Location, "", new { @class = "text-danger" })
</div>
</div>
<br />
<br />
<div class="form-group">
@Html.LabelFor(model => model.Mail, "E-mail", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Mail, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Mail, "", new { @class = "text-danger" })
</div>
</div>
<br />
<br />
<div class="form-group">
@Html.LabelFor(model => model.Telephone, "Telephone number", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Telephone, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Telephone, "", new { @class = "text-danger" })
</div>
</div>
<br />
<br />
<div class="form-group">
@Html.LabelFor(model => model.EnglishLang.NameLang, "English level", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("EngID", null, "--Choose level--", new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.EngID, "", new { @class = "text-danger" })
</div>
</div>
<br />
<br />
<div class="form-group">
@Html.LabelFor(model => model.Faculty.FacultyName, "Faculty", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("FacultyID", null, "Choose a faculty", new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.FacultyID, "", new { @class = "text-danger" })
</div>
</div>
<br />
<br />
<div class="form-group">
@Html.LabelFor(model => model.StartEduYear, "Entrance study year", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.StartEduYear, ViewBag.StartYearFaculty as IEnumerable<SelectListItem>)
@Html.ValidationMessageFor(model => model.StartEduYear, "", new { @class = "text-danger" })
</div>
</div>
<br />
<br />
<div class="form-group">
@Html.LabelFor(model => model.AvgScore, "Average score", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.AvgScore, ViewBag.AverScore as IEnumerable<SelectListItem>)
@Html.ValidationMessageFor(model => model.AvgScore, "", new { @class = "text-danger" })
</div>
</div>
<br />
<br />
<div class="form-group">
@Html.LabelFor(model => model.GradYear, "Graduation year", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.GradYear, ViewBag.GradYear as IEnumerable<SelectListItem>)
@Html.ValidationMessageFor(model => model.GradYear, "", new { @class = "text-danger" })
</div>
</div>
<p></p>
<br />
<br />
<p></p>
<p></p>
<div class="form-group">
@Html.LabelFor(model => model.WExp, "Work Experience", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextAreaFor(model => model.WExp, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.WExp, "", new { @class = "text-danger" })
</div>
</div>
<p></p>
<br />
<br />
<br />
<div class="form-group">
@Html.LabelFor(model => model.Interests, "Interests", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextAreaFor(model => model.Interests, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Interests, "", new { @class = "text-danger" })
</div>
</div>
<p></p>
<span>Did you have any additional trainings?</span>
<label for="chkYes">
<input type="radio" id="chkYes" name="chkPassPort" onclick=" ShowHideDiv() " />Yes
</label>
<label for="chkNo">
<input type="radio" id="chkNo" name="chkPassPort" onclick=" ShowHideDiv() " />No
</label>
<div class="form-group" id="dvPassport" style="display: none">
@Html.LabelFor(model => model.Trainings, "Trainings", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextAreaFor(model => model.Trainings, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Trainings, "", new { @class = "text-danger" })
</div>
</div>
<p></p>
<span>Did you have any projects recently?</span>
<label for="chkYes">
<input type="radio" id="chkYes1" name="chkPassPort1" onclick=" ShowHideDiv() " />Yes
</label>
<label for="chkNo">
<input type="radio" id="chkNo1" name="chkPassPort1" onclick=" ShowHideDiv() " />No
</label>
<div class="form-group" id="dvExample" style="display: none">
@Html.LabelFor(model => model.Projects, "Projects", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextAreaFor(model => model.Projects, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Projects, "", new { @class = "text-danger" })
</div>
</div>
<p></p>
<span>Have you been in some students organization?</span>
<label for="chkYes">
<input type="radio" id="chkYes2" name="chkPassPort2" onclick=" ShowHideDiv() " />Yes
</label>
<label for="chkNo">
<input type="radio" id="chkNo2" name="chkPassPort2" onclick=" ShowHideDiv() " />No
</label>
<div class="form-group" id="dvExample1" style="display: none">
@Html.LabelFor(model => model.StudOrg, "Organizations", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextAreaFor(model => model.StudOrg, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.StudOrg, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
<div class="g-recaptcha" data-sitekey="6LccVRcTAAAAADLo_LxSj4mQzt9jLrgbo5L9wZ-J"></div>
<div>
@if (TempData["recaptcha"] != null)
{
<p>@TempData["recaptcha"]</p>
}
</div>
</div>
<br />
<br />
<div class="col-md-offset-2 col-md-10">
<b></b>
<div class="form-group">
@Html.Recaptcha()
</div>
<br />
<br />
<br />
<br />
<input type="submit" value="Apply" class="btn btn-default" style="text-align: center" />
</div>
</div>
<p></p>
</div>
}
<script type="text/javascript">
function ShowHideDiv() {
var chkYes = document.getElementById("chkYes");
var chkYes1 = document.getElementById("chkYes1");
var chkYes2 = document.getElementById("chkYes2");
var dvPassport = document.getElementById("dvPassport");
var dvExample = document.getElementById("dvExample");
var dvExample1 = document.getElementById("dvExample1");
dvPassport.style.display = chkYes.checked ? "block" : "none";
dvExample.style.display = chkYes1.checked ? "block" : "none";
dvExample1.style.display = chkYes2.checked ? "block" : "none";
}
错误出现在这一行,即:
@Html.Recaptcha()
我已经将我的公钥和私钥插入到了Web.Config中。
而在我的控制器中,我有以下代码:
if (ModelState.IsValid)
{
var recaptchaHelper = this.GetRecaptchaVerificationHelper();
if (String.IsNullOrEmpty(recaptchaHelper.Response))
{
ModelState.AddModelError("", "Captcha answer cannot be empty.");
return View(employee);
}
var recaptchaResult = recaptchaHelper.VerifyRecaptchaResponse();
if (recaptchaResult != RecaptchaVerificationResult.Success)
{
ModelState.AddModelError("", "Incorrect captcha answer.");
}
DateTime myDateTime = DateTime.Now;
string sqlFormattedDate = myDateTime.ToString("yyyy-MM-dd HH:mm:ss");
int count = (from result1 in db.Employees.AsEnumerable() select result1).Count();
int maxEmployeeId = count > 0 ? db.Employees.Max(x => x.EmployeeID) + 1 : 1;
employee.EmployeeID = maxEmployeeId;
employee.CreatedTimeStamp = Convert.ToDateTime(sqlFormattedDate);
db.Employees.Add(employee);
db.SaveChanges();
return RedirectToAction("Success", "Employees");
return View(employee);
//string EncodedResponse = Request.Form["g-recaptcha-Response"];
//bool IsCaptchaValid = (ReCaptchaClass.Validate(EncodedResponse) == "True" ? true : false);
//if (IsCaptchaValid)
//{
//}
//else
//{
// TempData["recaptcha"] = "Please verify that you are not a robot.";
//}
}
我因为这个错误而疯狂,这是一个来自网络上某个人的实现,但我无法再找到它了(我已经重新安装了我的操作系统)...如果你能帮助我解决这个问题,那就太棒了!
recaptchaPublicKey
改为RecaptchaSiteKey
, 将recaptchaPrivateKey
改为RecaptchaSecretKey
。 - stanimirsp