Home MvcCaptcha MvcCaptcha getting started HOWTO (MVC2 & MVC3).

Login Form



MvcCaptcha getting started HOWTO (MVC2 & MVC3). PDF Print E-mail

Hello, guys (and girls?) and welcome.

 

Here we are going to do quick and brief explanation on how to enable captcha functionality in you ASP.NET MVC2 and MVC3 projects using MvcCaptcha library.

This guide is to use Visual Basic as an example language. I expect C# fellow should not have any problems with it. But in case you do - please feel free to contact me and ask.

Also, Razor view engine is used for MVC3 example.

 

1. Download MvcCaptcha library  and extract it.

2. MVCCaptcha libraries for MVC2 and MVC3 are located under corresponding folders. MvcCaptchaExtensionMVC2.dll is for MVC2 and MvcCaptchaExtensioMVC3.dll is for MVC3

3. Add reference to the appropriate MvcCaptcha library in your Visual Stidio ASP.NET MVC project:

For MVC2 project:

MVC2Reference

For MVC3 project:

MVC3Reference

 

4. Find web.config file in root folder of your ASP.NET MVC project:

 

5. Open it and add this line to "applicationSettings" sectionGroup of the file:

For MVC2 project:

<section name="SSB.Web.Mvc.MvcCaptcha.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />

 

For MVC3 project:

<section name="SSB.Web.Mvc.MvcCaptcha.Properties.Settings"  type="System.Configuration.ClientSettingsSection, System,  Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"  requirePermission="false" />

 

After this step your web.config "applicationSection" sectionGroup should look like:

For MVC2 Visual Basic project:

    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
      <section name="Your-app-namespace.My.MySettings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
      <section name="SSB.Web.Mvc.MvcCaptcha.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>

 

For MVC3 Visual Basic project:

    <sectionGroup name="applicationSettings"  type="System.Configuration.ApplicationSettingsGroup, System,  Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
       <section name="Your-app-namespace.My.MySettings"  type="System.Configuration.ClientSettingsSection, System,  Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"  requirePermission="false" />
      <section  name="SSB.Web.Mvc.MvcCaptcha.Properties.Settings"  type="System.Configuration.ClientSettingsSection, System,  Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"  requirePermission="false" />
    </sectionGroup>

 

6. Find MvcCaptchaExtensionMVC2.dll.config  or  MvcCaptchaExtensionMVC3.dll.config file among MvcCaptcha library extracted files.

Open it and copy text starting with <SSB.Web.Mvc.MvcCaptcha.Properties.Settings> line and ending with </SSB.Web.Mvc.MvcCaptcha.Properties.Settings> line to <applicationSettings> section of your ASP.NET MVC project's web.config file.

For version 2.0 of the library this text is applicable:

<SSB.Web.Mvc.MvcCaptcha.Properties.Settings>
    <setting name="MvcCaptchaWidth" serializeAs="String">
        <value>130</value>
    </setting>
    <setting name="MvcCaptchaHeight" serializeAs="String">
        <value>40</value>
    </setting>
  <setting name="MvcCaptchaTextLength" serializeAs="String">
    <value>6</value>
  </setting>
  <setting name="MvcCaptchaValidChars" serializeAs="String">
    <value>ABCDEFGHIJKLMNPQRSTUVWXYZ23456789abcdefghklmnpqrstuvw</value>
  </setting>
  <setting name="MvcCaptchaBackgroundGradientFromColor" serializeAs="String">
        <value>White</value>
    </setting>
    <setting name="MvcCaptchaBackgroundGradientToColor" serializeAs="String">
        <value>DarkGray</value>
    </setting>
    <setting name="MvcCaptchaFontFamilies" serializeAs="Xml">
        <value>
            <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xmlns:xsd="http://www.w3.org/2001/XMLSchema">
                <string>Verdana</string>
                <string>Times New Roman</string>
            </ArrayOfString>
        </value>
    </setting>
    <setting name="MvcCaptchaFontSize" serializeAs="String">
        <value>18</value>
    </setting>
    <setting name="MvcCaptchaFontColor" serializeAs="String">
        <value>Gray</value>
    </setting>
    <setting name="MvcCaptchaRandomLinesColor" serializeAs="String">
        <value>Gray</value>
    </setting>
    <setting name="MvcCaptchaRandomLinesNumber" serializeAs="String">
        <value>10</value>
    </setting>
    <setting name="MvcCaptchaHiddenInputId" serializeAs="String">
        <value>MvcCaptchaHiddenInputId</value>
    </setting>
    <setting name="MvcCaptchaReplayProtectionMode" serializeAs="String">
        <value>cache</value>
    </setting>
    <setting name="MvcCaptchaReplayProtectionTokenValidity" serializeAs="String">
        <value>00:10:00</value>
    </setting>
    <setting name="MvcCaptchaPassword" serializeAs="String">
        <value />
    </setting>
</SSB.Web.Mvc.MvcCaptcha.Properties.Settings>

 

7. Specify "MvcCaptchaPassword" setting value inside <SSB.Web.Mvc.MvcCaptcha.Properties.Settings> section. Without it MvcCaptcha will not work properly and will render warning message instead of Captcha picture.

For example you may set it like this:

            <setting name="MvcCaptchaPassword" serializeAs="String">
                 <value>MyVerySecretCaptchaPassword</value>
             </setting>

 

The password is required to make captcha generation unique for your application. We suggest to change it periodically to improve the captcha security.

 

8.  In any Models where you want to verify MvcCaptcha response validity, add ValidateMvcCaptcha attribute to the corresponding property.

For example:

    Private _captchaValue As String
    <Required(ErrorMessage:="*")> _
    <DisplayName("Captcha: please enter the text shown on the picture above")> _
    <ValidateMvcCaptcha(ErrorMessage:="Captcha response is not valid.")> _
    Public Property captchaValue() As String
        Get
            Return _captchaValue
        End Get
        Set(ByVal value As String)
            _captchaValue = value
        End Set
    End Property

 

9. In any View where you need captcha to be shown:

a) import  SSB.Web.Mvc.Html namespace:

MVC2 ASP.NET VB:

<%@ Import Namespace = "SSB.Web.Mvc.Html" %>

 

MVC3 Razor VB:

@Imports  SSB.Web.Mvc.Html

 

b)  add MvcCaptcha Html helper call:

MVC2 ASP.NET VB:

 <% =Html.MvcCaptcha()%>

 

MVC3 Razor VB:

@Html.MvcCaptcha()

 

c) add input element for model's captchaValue:

MVC2 ASP.NET VB:

                <div class="editor-label">
                    <% =Html.LabelFor(Function(model) model.captchaValue)%>
                </div>
                <div class="editor-field">
                    <% =Html.TextBoxFor(Function(model) model.captchaValue)%>
                    <% =Html.ValidationMessageFor(Function(model) model.captchaValue)%>
                </div>

 

MVC3 Razor VB:

                <div class="editor-label">
                    @Html.LabelFor(Function(model) model.captchaValue)
                </div>
                <div class="editor-field">
                    @Html.TextBoxFor(Function(model) model.captchaValue)
                    @Html.ValidationMessageFor(Function(model) model.captchaValue)
                </div>

 

 

That's it! Go and try it.

In case you want to customize MvcCaptcha, please look at the settings in <SSB.Web.Mvc.MvcCaptcha.Properties.Settings> section of web.config file.

The settings name are self-explanatory. But proper documentation is to be added.

 

Regards and enjoy.

 
cialis 40mg

Copyright © 2012 SmartSoftwareBits.com. All Rights Reserved.