How to fix security exception when using recaptcha on MVC site

Posted by camainc on Stack Overflow See other posts from Stack Overflow or by camainc
Published on 2010-03-20T21:59:40Z Indexed on 2010/03/20 22:01 UTC
Read the original article Hit count: 507

Filed under:
|

I followed this excellent blog post to implement recaptcha on my MVC site:

http://devlicio.us/blogs/derik_whittaker/archive/2008/12/02/using-recaptcha-with-asp-net-mvc.aspx

I converted the code to VB, and everything seems to compile ok. However, when the code gets to the place where the recapture is about to be generated, I get a security exception.

Here is the function where the exception occurs (on the last line in the function):

<Extension()> _
Public Function GenerateCaptcha(ByVal htmlHelper As HtmlHelper) As MvcHtmlString

    Dim captchaControl As New Recaptcha.RecaptchaControl
    With captchaControl
        .ID = "recaptcha"
        .Theme = "blackglass"
        .PublicKey = "6Lcv9AsAAAAAALCSZNRfWFmrKjw2AR-yuZAL84Bd"
        .PrivateKey = "6Lcv9AsAAAAAAHCbRujWcZzrY0z6G_HIMvFyYEPR"
    End With

    Dim htmlWriter As New HtmlTextWriter(New IO.StringWriter)
    captchaControl.RenderControl(htmlWriter)
    Return MvcHtmlString.Create(htmlWriter.InnerWriter.ToString())

End Function

The exception is this:

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.Web.AspNetHostingPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

Has anyone else seen this exception, and if so, how did you fix it?

Thanks

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about recaptcha