OpenCAPTCHA.org

Sample Challenge-Answer Service

Modified: 2010/01/29 07:49 by 195.211.172.252 - Categorized as: Samples
The following is a sample Challenge-Answer Service per the v0.1 rough draft spec. It uses C# 3.0 with ASP.NET MVC. The URL for this sample when implemented is "http://[hostname]/ChallengeAnswer/", and the output is:

{
    challenge : "Enter this word: orange",
    answer : ["orange"],
    caseSensitive : false,
    format : "text",
    expires : "2009-08-05T07:53:23"
}

public class ChallengeAnswerController : Controller
{
    //
    // GET: /ChallengeAnswer/

    public ActionResult Index()
    {
        var ret = new ChallengeAnswer
                      {
                          challenge = "Enter this word: orange",
                          answer = new[] {"orange"},
                          caseSensitive = false,
                          format = ChallengeFormat.text.ToString(), // or just "text"
                          expires = System.DateTime.UtcNow.AddHours(24)
                              .ToString("yyyy-MM-ddTHH:mm:ss.fffZ",
                                        System.Globalization
                                            .CultureInfo.InvariantCulture)
                      };

        var type = "json";
        if (Request["type"] != null) type = Request["type"];
        switch (type.ToLower())
        {
            case "jsonp":
                return Jsonp(ret);
            case "json":
                return Json(ret);
            case "xml":
                return Xml(ret);
        }
        return View();
    }

    public JavaScriptResult Jsonp(object val)
    {
        var ret = new JavaScriptResult();
        var jsds = new System.Web.Script.Serialization.JavaScriptSerializer();
        var jsval = jsds.Serialize(val);
        ret.Script = Request["callback"] + "(" + jsval + ")";
        return ret;
    }

    public XmlResult Xml(object val)
    {
        var xr = new XmlResult();
        if (val is string) xr.Xml = (string)val;
        else
        {
            var xs = new System.Xml.Serialization.XmlSerializer(val.GetType());
            var ms = new System.IO.MemoryStream();movie downloads 2010 HD
            xs.Serialize(ms, val);
            ms.Seek(0, System.IO.SeekOrigin.Begin);
            var sr = new System.IO.StreamReader(ms);
            xr.Xml = sr.ReadToEnd();
        }
        return xr;
    }

    public class XmlResult : ActionResult
    {
        public string Xml { get; set; }

        public override void ExecuteResult(ControllerContext context)
        {
            context.HttpContext.Response.ContentType = "text/xml";
            context.HttpContext.Response.Write(Xml);
        }
    }

}

public enum ChallengeFormat
{
    image,
    text,
    html,
    htmlInput,
    canvasJs,
    swf,
    xap
}

[Serializable]
[XmlType("challengeAnswer")]
public class ChallengeAnswer
{
    public string format;
    public string challenge;
    public string[] answer;
    public bool caseSensitive;
    public string expires; // TimeSpan-formatted
}

ScrewTurn Wiki version 2.0.37. Some of the icons created by FamFamFam.