I have an error:
System.InvalidOperationException: Public key cannot be null or empty.
my Controller page Employees/Create looks like:
@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";
}
Error comes in the row where is
@Html.Recaptcha()
I've inserted my public and private key into Web.Config
And in my controller I have:
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.";
//}
}
I am going crazy because of this error, this was an implementation by some guy from the Net, but I cannot find it anymore (I've reinstalled my OS)..if you be so kind to help me to figure it out it would be awesome!
In case that you have just updated the nuget package, look for duplicate settings initialization like the following at the end of your appSettings section :
<add key="recaptchaPublicKey" value="" /><add key="recaptchaPrivateKey" value="" /><add key="recaptchaApiVersion" value="1" />
in your web.config.
When you update the recaptcha package it will automatically append this row in your appSettings section even if you have already set your settings - thus overriding them.
Otherwise - set your keys here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With