Sunday, October 2, 2022
HomeC#Add Captcha in your ASP.NET Core Web site

Add Captcha in your ASP.NET Core Web site


These days, if in case you have a type that’s publicly accessible, you could have a captcha with a purpose to cease bots to put up spammy information.

Add your web site to the reCaptcha console

ReCaptcha is the preferred captcha service on the Web. It’s supplied totally free by Google.

You should create an account and add your web site to the admin console.

If you add your area, just be sure you choose the reCAPTCHA V2 choice (I’m not a robotic Checkbox).

Add site to Recaptcha Console

Don’t neglect to repeat in a notepad the Web site Key and the key key.  We’ll want these two values later.

Within the settings of the web site that you simply’ve added, you’ll be able to set how tough the captcha shall be.

reCAPTCHA Level of Protection

Set up a NuGet bundle that helps reCAPTCHA

I personally like to make use of Paul Miami’s reCAPTCHA library.

Set up the NuGet Package deal:

Set up-Package deal PaulMiami.AspNetCore.Mvc.Recaptcha

Configure reCAPTCHA in ASP.NET Core

  1. Add the positioning and secret key within the appsettings file:
    {
      "Logging": {
        "LogLevel": {
          "Default": "Info",
          "Microsoft.AspNetCore": "Warning"
        }
      },
      "AllowedHosts": "*",
      "Recaptcha": {
        "SiteKey": "Your web site key",
        "SecretKey": "Your secret key"
      }
    }
  2. Add the service in Program/Startup Class:
    companies.AddRecaptcha(new RecaptchaOptions
    {
        SiteKey = builder.Configuration["Recaptcha:SiteKey"],
        SecretKey = builder.Configuration["Recaptcha:SecretKey"]
    });
  3. Import the tag helper. Add the next line in _ViewImports.cshtml file:
    @addTagHelper *, PaulMiami.AspNetCore.Mvc.Recaptcha
  4. Place the tag within the views or pages the place you need to have reCaptcha:
    <recaptcha/>

    Add the jQuery libraries for validation and the Recaptcha script:

    <script src="https://programmingcsharp.com/add-captcha-in-asp-net-core-website/~/lib/jquery/dist/jquery.js"></script>
    <script src="~/lib/jquery-validation/dist/jquery.validate.js"></script>
    <script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js"></script>
    <recaptcha-script/>
  5.  Embellish the actions with the attribute Validate Recaptcha:

For Razor Pages, you could beautify the web page class as a substitute of the strategy.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments