# Abp.VerificationCode

ABP version (opens new window) NuGet (opens new window) NuGet Download (opens new window) Discord online (opens new window) GitHub stars (opens new window)

An ABP module to generate and verify verification codes.

# Installation

  1. Install the following NuGet packages. (see how (opens new window))

    • EasyAbp.Abp.VerificationCode
    • (Optional) EasyAbp.Abp.VerificationCode.Identity
  2. Add DependsOn(typeof(AbpVerificationCodeXxxModule)) attribute to configure the module dependencies. (see how (opens new window))

# Usage

# Generate and Validate

  1. Generate a verification code.

    var verificationCodeManager = ServiceProvider.GetRequiredService<IVerificationCodeManager>();
    
    var code = await verificationCodeManager.GenerateAsync(
        codeCacheKey: $"DangerousOperationPhoneVerification:{phoneNumber}",
        codeCacheLifespan: TimeSpan.FromMinutes(3),
        configuration: new VerificationCodeConfiguration());
    
    await _smsSender.SendAsync(new SmsMessage(phoneNumber, $"Your code is: {code}"));
    // you can also use the EasyAbp.NotificationService module and send the code to users.
    
  2. Validate a verification code.

    var verificationCodeManager = ServiceProvider.GetRequiredService<IVerificationCodeManager>();
    
    var result = await verificationCodeManager.ValidateAsync(
        codeCacheKey: $"DangerousOperationPhoneVerification:{phoneNumber}",
        verificationCode: code,
        configuration: new VerificationCodeConfiguration());
    

# Use as UserManager Token Providers

Please install the EasyAbp.Abp.VerificationCode.Identity module, it registers the token providers that uses IVerificationCodeManager (see (opens new window)). And you can replace the DefaultIdentityVerificationCodeConfigurationProvider (opens new window) to customize the verification code generation rules.

# Q&A

# How to Change the Code Generation Strategy

You can instantiate a custom VerificationCodeConfiguration (opens new window), for example:

var configuration = new VerificationCodeConfiguration(
    length: 6,
    chars: "0123456789abcdefghijklmnopqrstuvwxyz",
    equivalentCharsMaps: new Dictionary<char, IEnumerable<char>>
    {
        {'0', new[] {'o', 'O'}},
        {'1', new[] {'l', 'L'}},
    }); // "oL2345" will also be verified if the correct code is "012345"

# Road map

Todo.

Last Updated: 11/19/2022, 8:47:32 AM