Recaptcha

Discussion in 'Site Programming, Development and Design' started by Fehim, Sep 4, 2010.

  1. Hello everyone,

    I'm trying to add "Recaptcha (http://code.google.com/intl/hr-HR/apis/recaptcha)" to my web site.

    On my local machine recaptcha works fine, but when I deploy it on remote server I'm getting error.

    It's about instantiating object of class HttpWebRequest in recaptcha.

    P.S. This forum use recaptcha in registration form for new members :) !

    Code:
    Server Error in '/' Application.
    
    Security Exception
    
    Description: The application attempted to perform an operation not allowed by the security policy.  To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file. 
    
    Exception Details: System.Security.SecurityException: Request for the permission of type 'System.Net.WebPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
    
    Source Error: 
    
    
    Line 18:       Recaptcha.RecaptchaControl recpatcha = (Login1.FindControl("recaptcha") as Recaptcha.RecaptchaControl);
    Line 19:       recpatcha.Validate();
    Line 20:       if (!recpatcha.IsValid)
    Line 21:       {
    
    Stack Trace: 
    
    
    [SecurityException: Request for the permission of type 'System.Net.WebPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.]
       System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet) +0
       System.Security.CodeAccessPermission.Demand() +58
       System.Net.HttpWebRequest..ctor(Uri uri, ServicePoint servicePoint) +147
       System.Net.HttpRequestCreator.Create(Uri Uri) +26
       System.Net.WebRequest.Create(Uri requestUri, Boolean useUriBase) +216
       System.Net.WebRequest.Create(String requestUriString) +44
       Recaptcha.RecaptchaValidator.Validate() in RecaptchaValidator.cs:113
       Recaptcha.RecaptchaControl.Validate() in RecaptchaControl.cs:301
    
    

    This is source code of recaptcha validator:

    Code:
    // Copyright (c) 2007 Adrian Godong, Ben Maurer
    //
    // Permission is hereby granted, free of charge, to any person obtaining a copy
    // of this software and associated documentation files (the "Software"), to deal
    // in the Software without restriction, including without limitation the rights
    // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    // copies of the Software, and to permit persons to whom the Software is
    // furnished to do so, subject to the following conditions:
    //
    // The above copyright notice and this permission notice shall be included in
    // all copies or substantial portions of the Software.
    //
    // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    // THE SOFTWARE.
    
    using System;
    using System.Diagnostics;
    using System.IO;
    using System.Net;
    using System.Net.Sockets;
    using System.Text;
    using System.Web;
    
    namespace Recaptcha
    {
        /// <summary>
        /// Calls the reCAPTCHA server to validate the answer to a reCAPTCHA challenge. Normally,
        /// you will use the RecaptchaControl class to insert a web control on your page. However
        /// </summary>
        public class RecaptchaValidator
        {
            private const string VerifyUrl = "http://www.google.com/recaptcha/api/verify";
    
            private string privateKey;
            private string remoteIp;
    
            private string challenge;
            private string response;
    
            private IWebProxy proxy;
    
            public string PrivateKey
            {
                get { return this.privateKey; }
                set { this.privateKey = value; }
            }
    
            public string RemoteIP
            {
                get
                {
                    return this.remoteIp;
                }
    
                set
                {
                    IPAddress ip = IPAddress.Parse(value);
    
                    if (ip == null ||
                        (ip.AddressFamily != AddressFamily.InterNetwork &&
                        ip.AddressFamily != AddressFamily.InterNetworkV6))
                    {
                        throw new ArgumentException("Expecting an IP address, got " + ip);
                    }
    
                    this.remoteIp = ip.ToString();
                }
            }
    
            public string Challenge
            {
                get { return this.challenge; }
                set { this.challenge = value; }
            }
    
            public string Response
            {
                get { return this.response; }
                set { this.response = value; }
            }
    
            public IWebProxy Proxy
            {
                get { return this.proxy; }
                set { this.proxy = value; }
            }
    
            private void CheckNotNull(object obj, string name)
            {
                if (obj == null)
                {
                    throw new ArgumentNullException(name);
                }
            }
    
            public RecaptchaResponse Validate()
            {
                this.CheckNotNull(this.PrivateKey, "PrivateKey");
                this.CheckNotNull(this.RemoteIP, "RemoteIp");
                this.CheckNotNull(this.Challenge, "Challenge");
                this.CheckNotNull(this.Response, "Response");
    
                if (this.challenge == string.Empty || this.response == string.Empty)
                {
                    return RecaptchaResponse.InvalidSolution;
                }
    
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(VerifyUrl);
                  
    
                request.ProtocolVersion = HttpVersion.Version10;
                request.Timeout = 30 * 1000 /* 30 seconds */;
                request.Method = "POST";
                request.UserAgent = "reCAPTCHA/ASP.NET";
                if (this.proxy != null)
                {
                    request.Proxy = this.proxy;
                }
    
                request.ContentType = "application/x-www-form-urlencoded";
    
                string formdata = String.Format(
                    "privatekey={0}&remoteip={1}&challenge={2}&response={3}",
                                        HttpUtility.UrlEncode(this.PrivateKey),
                                        HttpUtility.UrlEncode(this.RemoteIP),
                                        HttpUtility.UrlEncode(this.Challenge),
                                        HttpUtility.UrlEncode(this.Response));
    
                byte[] formbytes = Encoding.ASCII.GetBytes(formdata);
    
                using (Stream requestStream = request.GetRequestStream())
                {
                    requestStream.Write(formbytes, 0, formbytes.Length);
                }
    
                string[] results;
    
                try
                {
                    using (WebResponse httpResponse = request.GetResponse())
                    {
                        using (TextReader readStream = new StreamReader(httpResponse.GetResponseStream(), Encoding.UTF8))
                        {
                            results = readStream.ReadToEnd().Split(new string[] { "\n", "\\n" }, StringSplitOptions.RemoveEmptyEntries);
                        }
                    }
                }
                catch (WebException ex)
                {
                    EventLog.WriteEntry("Application", ex.Message, EventLogEntryType.Error);
                    return RecaptchaResponse.RecaptchaNotReachable;
                }
    
                switch (results[0])
                {
                    case "true":
                        return RecaptchaResponse.Valid;
                    case "false":
                        return new RecaptchaResponse(false, results[1].Trim(new char[] { '\'' }));
                    default:
                        throw new InvalidProgramException("Unknown status response.");
                }
            }
        }
    }
    
     
  2. Last edited by a moderator: Oct 14, 2015

Share This Page