CAPTCHAfa
نویسنده: بهروز راد
تاریخ: ۱۳۹۱/۰۵/۱۵ ۱۲:۴۶
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Captchafa demo for ASP.NET applications
// by: Behrouz Rad
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
namespace Captcha
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Web;
public class Captchafa
{
private static readonly string PRIVATE_KEY = "your private key";
private static readonly string PUBLIC_KEY = "your public key";
private static readonly string CAPTCHAFA_API_SERVER = "http://www.captchafa.com/api";
private static readonly string CAPTCHAFA_VERIFY_SERVER = "http://www.captchafa.com/api/verify/";
private IDictionary<string, string> CaptchafaData
{
get
{
HttpContext httpContext = HttpContext.Current;
string remoteIp = httpContext.Request.ServerVariables["REMOTE_ADDR"];
string challenge = httpContext.Request.Form["captchafa_challenge_field"];
string response = httpContext.Request.Form["captchafa_response_field"];
IDictionary<string, string> data = new Dictionary<string, string>()
{
{"privatekey" , PRIVATE_KEY },
{"remoteip" , remoteIp },
{"challenge" , challenge },
{"response" , response }
};
return data;
}
}
public static string CaptchafaGetHtml()
{
return string.Format("<script type=\"text/javascript\" src=\"{0}/?challenge&k={1}\"></script>", CAPTCHAFA_API_SERVER, PUBLIC_KEY);
}
public bool IsAnswerCorrect()
{
string dataToSend = this.CaptchafaPrepareDataToSend(this.CaptchafaData);
string result = this.CaptchafaPostResponse(dataToSend);
return result.StartsWith("true");
}
private string CaptchafaPrepareDataToSend(IDictionary<string, string> data)
{
string result = string.Empty;
StringBuilder sb = new StringBuilder();
foreach (var item in data)
{
sb.AppendFormat("{0}={1}&", item.Key, HttpUtility.UrlEncode(item.Value.Replace(@"\", string.Empty)));
}
result = sb.ToString();
sb = null;
result = result.Substring(0, result.LastIndexOf("&"));
return result;
}
private string CaptchafaPostResponse(string data)
{
StreamReader reader = null;
Stream dataStream = null;
WebResponse response = null;
string responseFromServer = string.Empty;
try
{
WebRequest request = WebRequest.Create(CAPTCHAFA_VERIFY_SERVER);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
byte[] byteData = Encoding.UTF8.GetBytes(data);
request.ContentLength = byteData.Length;
dataStream = request.GetRequestStream();
dataStream.Write(byteData, 0, byteData.Length);
dataStream.Close();
response = request.GetResponse();
dataStream = response.GetResponseStream();
reader = new StreamReader(dataStream);
responseFromServer = reader.ReadToEnd();
}
finally
{
if (reader != null)
{
reader.Close();
}
if (dataStream != null)
{
dataStream.Close();
}
if (response != null)
{
response.Close();
}
}
return responseFromServer;
}
}
}
if (!IsPostBack)
{
litCaptcha.Text = Captchafa.CaptchafaGetHtml();
}
Captchafa captchaFa = new Captchafa();
bool isAnswerCorrect = captchaFa.IsAnswerCorrect();
if (isAnswerCorrect)
{
// پاسخ صحیح است
}
else
{
// پاسخ صحیح نیست
}
public static class CaptchaHelper
{
public static MvcHtmlString Captchafa(this HtmlHelper htmlHelper)
{
return MvcHtmlString.Create(Captcha.Captchafa.CaptchafaGetHtml());
}
}
[HttpPost]
[ActionName("Index")]
public ViewResult CaptchaCheck()
{
Captchafa captchaFa = new Captchafa();
bool isAnswerCorrect = captchaFa.IsAnswerCorrect();
if (isAnswerCorrect)
{
ViewBag.IsAnswerCorrect = true;
}
else
{
ViewBag.IsAnswerCorrect = false;
}
return View();
}
@using CaptchafaDemoMvc.Helpers;
@{
ViewBag.Title = "Index";
}
<form action="/" method="post">
@Html.Captchafa();
<input type="submit" id="btnCaptchafa" name="btnCaptchafa" value="آزمایش" />
@{
bool isAnswerExists = ViewBag.IsAnswerCorrect != null;
}
@if (isAnswerExists)
{
if ((bool)ViewBag.IsAnswerCorrect == true)
{
<span id="lblResult">پاسخ صحیح است</span>
}
else
{
<span id="lblResult">پاسخ صحیح نیست</span>
}
}
</form>