How to do google captcha in form submit in mvc controller
string EncodedResponse = Request.Form["g-Recaptcha-Response"];
ReCaptchaClass.Validate(EncodedResponse, Request)
public class ReCaptchaClass
{
public static string Validate(string EncodedResponse, HttpRequestBase request)
{
var client = new WebClient();
if (request.Url.Host == "www-mot-gov-sg.cwp.sg" || request.Url.Host == "www.mot.gov.sg")
{
client.Proxy.Credentials = new NetworkCredential("www.mot.gov.sg", "NdjdrkhtvTze");
}
else if (request.Url.Host == "www-mot-gov-sg.cwp-stg.sg")
{
client.Proxy.Credentials = new NetworkCredential("www.mot.gov.sg", "YcJyMvMqxgr3");
}
string PrivateKey = "6LdmLEEUAAAAALihIQfuD--P0wVUD3Mieb2lex81";
if(request.Url.Host == "localhost")
{
PrivateKey = "6Lf5BD4UAAAAAOqvZqtBlhHtqZSx7GZcfqOOJj-P";
}
var GoogleReply = client.DownloadString(string.Format("https://www.google.com/recaptcha/api/siteverify?secret={0}&response={1}", PrivateKey, EncodedResponse));
var captchaResponse = Newtonsoft.Json.JsonConvert.DeserializeObject<ReCaptchaClass>(GoogleReply);
return captchaResponse.Success;
}
[JsonProperty("success")]
public string Success
{
get { return m_Success; }
set { m_Success = value; }
}
private string m_Success;
[JsonProperty("error-codes")]
public List<string> ErrorCodes
{
get { return m_ErrorCodes; }
set { m_ErrorCodes = value; }
}
private List<string> m_ErrorCodes;
}
ReCaptchaClass.Validate(EncodedResponse, Request)
public class ReCaptchaClass
{
public static string Validate(string EncodedResponse, HttpRequestBase request)
{
var client = new WebClient();
if (request.Url.Host == "www-mot-gov-sg.cwp.sg" || request.Url.Host == "www.mot.gov.sg")
{
client.Proxy.Credentials = new NetworkCredential("www.mot.gov.sg", "NdjdrkhtvTze");
}
else if (request.Url.Host == "www-mot-gov-sg.cwp-stg.sg")
{
client.Proxy.Credentials = new NetworkCredential("www.mot.gov.sg", "YcJyMvMqxgr3");
}
string PrivateKey = "6LdmLEEUAAAAALihIQfuD--P0wVUD3Mieb2lex81";
if(request.Url.Host == "localhost")
{
PrivateKey = "6Lf5BD4UAAAAAOqvZqtBlhHtqZSx7GZcfqOOJj-P";
}
var GoogleReply = client.DownloadString(string.Format("https://www.google.com/recaptcha/api/siteverify?secret={0}&response={1}", PrivateKey, EncodedResponse));
var captchaResponse = Newtonsoft.Json.JsonConvert.DeserializeObject<ReCaptchaClass>(GoogleReply);
return captchaResponse.Success;
}
[JsonProperty("success")]
public string Success
{
get { return m_Success; }
set { m_Success = value; }
}
private string m_Success;
[JsonProperty("error-codes")]
public List<string> ErrorCodes
{
get { return m_ErrorCodes; }
set { m_ErrorCodes = value; }
}
private List<string> m_ErrorCodes;
}
Comments
Post a Comment