I am using this code to implement Google's reCaptcha on my ASP.Net MVC 5 page:
https://www.c-sharpcorner.com/blogs/google-recaptcha-in-asp-net-mvc
public class ValidateGoogleCaptchaAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
const string urlToPost = "https://www.google.com/recaptcha/api/siteverify";
var captchaResponse = filterContext.HttpContext.Request.Form["g-recaptcha-response"];
if (string.IsNullOrWhiteSpace(captchaResponse)) AddErrorAndRedirectToGetAction(filterContext);
var validateResult =
ValidateFromGoogle(urlToPost, GoogleReCaptchaVariables.ReCaptchaSecretKey, captchaResponse);
if (!validateResult.Success) AddErrorAndRedirectToGetAction(filterContext);
base.OnActionExecuting(filterContext);
}
The problem with this code is that the web site in question does not have access to the Internet and cannot directly call Google's API, it has to go through an internal service, IReCaptcha, which is injected into the whole system via Unity.MVC:
container.RegisterType<IReCaptcha, ReCaptcha>();
The question is: How does one go about injecting IReCaptcha into ValidateGoogleCaptchaAttribute?
It seems the only solution is to get the current DependencyResolver and get the service manually:
var reCaptcha = DependencyResolver.Current.GetService(typeof(IReCaptcha)) as IReCaptcha;
I believe this should work anywhere.