DNTCaptcha.Core

repository·master·Indexed 42 days ago

https://github.com/vahidn/dntcaptcha.core

A captcha generator and validator for ASP.NET Core applications. It supports various storage providers (Session, MemoryCache, Cookie, and Distributed Cache), multiple display modes like NumberToWord and SumOfTwoNumbers, and integrates with UI frameworks like Bootstrap. It includes a tag helper for Razor views and a [ValidateDNTCaptcha] attribute for controller validation.

Tokens
8.3K
Snippets
30
Records
45
Agent score
52%

What's inside DNTCaptcha.Core

  1. What is jQuery Unobtrusive Validation

    master
    jQuery Unobtrusive Validation is a library that complements the standard jQuery Validation plugin. It allows you to specify validation rules and options directly within HTML5 data-* attributes on your elements, rather than writing manual JavaScript validation logic. This approach is commonly used in ASP.NET Core applications to bridge the gap between server-side model metadata and client-side validation.
  2. How to choose a correct storage mode

    master

    Choosing a storage provider depends on your deployment architecture:

    • Distributed/Stateless Environments (e.g., Azure Web Roles, Kubernetes): Avoid UseSessionStorageProvider() or UseMemoryCacheStorageProvider() because session state is stored in local RAM and will be lost if the instance moves or restarts. Instead, use UseCookieStorageProvider() or UseDistributedCacheStorageProvider() (e.g., with Redis) to ensure state persists across different server instances.
  3. Understand Bootstrap 5 file structure

    master

    The Bootstrap distribution includes compiled CSS and JS, minified versions, and source maps.

    Key components:

    • bootstrap.*: Compiled CSS and JS.
    • bootstrap.min.*: Minified versions for production.
    • bootstrap.*.map: Source maps for browser developer tools.
    • bootstrap.bundle.js / bootstrap.bundle.min.js: JS files that include Popper bundled inside.
    • css/: Contains grid, reboot, and utility styles, including RTL (Right-to-Left) support.
    • js/: Contains ESM (ES Modules) and standard JS builds.
    bootstrap/
    ├── css/
    │   ├── bootstrap-grid.css
    │   ├── bootstrap-grid.min.css
    │   ├── bootstrap-grid.rtl.min.css
    │   ├── bootstrap-reboot.min.css
    │   ├── bootstrap-utilities.min.css
    │   ├── bootstrap.css
    │   ├── bootstrap.min.css
    │   └── bootstrap.rtl.min.css
    └── js/
        ├── bootstrap.bundle.js
        ├── bootstrap.bundle.min.js
        ├── bootstrap.esm.js
        ├── bootstrap.esm.min.js
        ├── bootstrap.js
        └── bootstrap.min.js
  4. Run the DntCaptchaAngularClient demo application

    master

    To run the full demonstration environment consisting of the DNTCaptcha.TestApiApp backend and the DntCaptchaAngularClient frontend, follow these steps:

    1. Start the Backend: Run the DNTCaptcha.TestApiApp project using your IDE (Visual Studio or Rider) or via the CLI using dotnet watch run.
    2. Install Frontend Dependencies: Navigate to the DNTCaptcha.AngularClient/ directory and run npm install.
    3. Start the Frontend: Inside the DNTCaptcha.AngularClient/ directory, execute ng serve or npm run start.
    4. Access the App: Open your browser and navigate to http://localhost:4200.

    Note: The Angular client is designed to work with the DNTCaptcha.TestApiApp backend.

    # 1. Start backend
    dotnet watch run
    
    # 2. Install frontend dependencies
    cd DNTCaptcha.AngularClient/
    npm install
    
    # 3. Start frontend
    ng serve
  5. Improve accessibility with errorElement

    master

    By default, the plugin outputs error messages in a <label> element. To improve screen reader support and avoid having multiple <label> elements for a single input, use the errorElement parameter in the validate() method.

    When you specify an element (e.g., "span"), the plugin automatically adds aria-describedby to the input field and links it to the error element, ensuring better programmatic association for assistive technologies.

    ```js
    // Configuration
    $("#myform").validate({
      errorElement: "span"
    });

    <!-- Resulting HTML Structure --> <label for="name">Name</label> <input id="name" aria-describedby="unique-id-here"> <span class="error" id="unique-id-here">This field is required</span>

  6. Install and include the jQuery Validation Plugin

    master

    The jQuery Validation Plugin provides drop-in validation for existing forms. You can download prebuilt files from jqueryvalidation.org or obtain the latest development files by building the repository using grunt.

    To use the plugin, include both jQuery and jquery.validate.js on your page, then call the .validate() method on your form selector.

    <form>
    	<input required>
    </form>
    <script src="jquery.js"></script>
    <script src="jquery.validate.js"></script>
    <script>
        $("form").validate();
    </script>
  7. Configure Forwarded Headers for Reverse Proxies

    master

    If your application is running behind a reverse proxy (like a load balancer), you must add the forwarded headers middleware to ensure correct request processing. Configure ForwardedHeadersOptions with the appropriate headers and known proxy IP addresses.

    services.Configure<ForwardedHeadersOptions>(options =>
    {
      options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
      options.KnownProxies.Add(IPAddress.Parse("my load balancer ip 1"));
      options.KnownProxies.Add(IPAddress.Parse("my load balancer ip 2"));
    });
  8. Use DNTCaptcha with Angular or Pure JavaScript

    master

    DNTCaptcha is compatible with modern Single Page Application (SPA) architectures:

    • Angular: Use the dntcaptcha-angular-client package. A sample implementation includes a server-side controller, a dedicated Angular component, and a login component integration.
    • Pure JavaScript: The library can be used with any pure JavaScript application via API calls.