I was facing an issue of dom parsing in my website. I finally resolved it by using Google Captcha V3.
Step 1: Get your keys from
https://developers.google.com/recaptcha/intro
Step 2: Use the below example code:
.ASPX
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Captchav3._Default" %>
<asp:Content runat="server" ID="FeaturedContent" ContentPlaceHolderID="FeaturedContent">
<section class="featured">
<div class="content-wrapper">
</div>
</section>
</asp:Content>
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
<h3>Login Form</h3>
<asp:HiddenField ID="LoginId" runat="server" />
Login:<asp:TextBox ID="txtLogin" runat="server"></asp:TextBox><br />
Password:<asp:TextBox ID="txtPassword" runat="server"></asp:TextBox><br />
<asp:Button ID="btnLogin" runat="server" Text="Login" OnClick="btnLogin_Click" />
<script src="https://www.google.com/recaptcha/api.js?render=6Lc7ctQUXXXXXzgKdup36qsMS-y_Hc6JCwAn_Bd"></script>
<script>
grecaptcha.ready(function () {
grecaptcha.execute('6Lc7ctQUXXXXXzgKdup36qsMS-y_Hc6JCwAn_Bd', { action: 'umslogin' }).then(function (token) {
document.getElementById('<%= LoginId.ClientID%>').value = token;
});
});
</script>
</asp:Content>
C#
public partial class _Default : Page
{
private static string Token = string.Empty;
protected void Page_Load(object sender, EventArgs e)
{
Token = LoginId.Value;
}
private static ResponseToken response = new ResponseToken();
private string CaptchaVerify()
{
if (response.score == 0)
{
var responseString = RecaptchaVerify(Token);
response = JsonConvert.DeserializeObject<ResponseToken>(responseString.Result);
}
return JsonConvert.SerializeObject(response);
//return "";
}
private static string apiAddress = "https://www.google.com/recaptcha/api/siteverify";
private static async Task<string> RecaptchaVerify(string recaptchaToken)
{
string url = apiAddress + "?secret=6Lc7ctQUXXXXXL--lngI_gIyRFyToplpKvZmMVlj&response=" + recaptchaToken;
using (HttpClient httpClient = new HttpClient())
{
try
{
string responseString = httpClient.GetStringAsync(url).Result;
return responseString;
//dynamic json = JsonConvert.DeserializeObject(responseString);
//return json.success;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}
protected void btnLogin_Click(object sender, EventArgs e)
{
dynamic o = JsonConvert.DeserializeObject(CaptchaVerify());
if (o.Success.Value)
{
if (o.score.Value > 0.3)
{
Response.Write("<h1>Valid Request</h1>");
if (txtLogin.Text == "admin" && txtPassword.Text == "admin")
{
Response.Write("Logged In Successfully");
}
}
else
{
Response.Write("<h1>Not a Valid Request</h1>");
Response.StatusCode = 403;
}
}
else
{
Response.Write("<h1>Not a Valid Request</h1>");
Response.StatusCode = 403;
}
}
}
public class ResponseToken
{
public DateTime challenge_ts { get; set; }
public float score { get; set; }
public List<string> ErrorCodes { get; set; }
public bool Success { get; set; }
public string hostname { get; set; }
}
Step 1: Get your keys from
https://developers.google.com/recaptcha/intro
Step 2: Use the below example code:
.ASPX
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Captchav3._Default" %>
<asp:Content runat="server" ID="FeaturedContent" ContentPlaceHolderID="FeaturedContent">
<section class="featured">
<div class="content-wrapper">
</div>
</section>
</asp:Content>
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
<h3>Login Form</h3>
<asp:HiddenField ID="LoginId" runat="server" />
Login:<asp:TextBox ID="txtLogin" runat="server"></asp:TextBox><br />
Password:<asp:TextBox ID="txtPassword" runat="server"></asp:TextBox><br />
<asp:Button ID="btnLogin" runat="server" Text="Login" OnClick="btnLogin_Click" />
<script src="https://www.google.com/recaptcha/api.js?render=6Lc7ctQUXXXXXzgKdup36qsMS-y_Hc6JCwAn_Bd"></script>
<script>
grecaptcha.ready(function () {
grecaptcha.execute('6Lc7ctQUXXXXXzgKdup36qsMS-y_Hc6JCwAn_Bd', { action: 'umslogin' }).then(function (token) {
document.getElementById('<%= LoginId.ClientID%>').value = token;
});
});
</script>
</asp:Content>
C#
public partial class _Default : Page
{
private static string Token = string.Empty;
protected void Page_Load(object sender, EventArgs e)
{
Token = LoginId.Value;
}
private static ResponseToken response = new ResponseToken();
private string CaptchaVerify()
{
if (response.score == 0)
{
var responseString = RecaptchaVerify(Token);
response = JsonConvert.DeserializeObject<ResponseToken>(responseString.Result);
}
return JsonConvert.SerializeObject(response);
//return "";
}
private static string apiAddress = "https://www.google.com/recaptcha/api/siteverify";
private static async Task<string> RecaptchaVerify(string recaptchaToken)
{
string url = apiAddress + "?secret=6Lc7ctQUXXXXXL--lngI_gIyRFyToplpKvZmMVlj&response=" + recaptchaToken;
using (HttpClient httpClient = new HttpClient())
{
try
{
string responseString = httpClient.GetStringAsync(url).Result;
return responseString;
//dynamic json = JsonConvert.DeserializeObject(responseString);
//return json.success;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}
protected void btnLogin_Click(object sender, EventArgs e)
{
dynamic o = JsonConvert.DeserializeObject(CaptchaVerify());
if (o.Success.Value)
{
if (o.score.Value > 0.3)
{
Response.Write("<h1>Valid Request</h1>");
if (txtLogin.Text == "admin" && txtPassword.Text == "admin")
{
Response.Write("Logged In Successfully");
}
}
else
{
Response.Write("<h1>Not a Valid Request</h1>");
Response.StatusCode = 403;
}
}
else
{
Response.Write("<h1>Not a Valid Request</h1>");
Response.StatusCode = 403;
}
}
}
public class ResponseToken
{
public DateTime challenge_ts { get; set; }
public float score { get; set; }
public List<string> ErrorCodes { get; set; }
public bool Success { get; set; }
public string hostname { get; set; }
}
No comments:
Post a Comment
Comments are welcome, Please join me on my Linked In account
http://in.linkedin.com/pub/ritesh-tandon/21/644/33b