Skip to content

EasyAbp/Abp.VerificationCode

Repository files navigation

Abp.VerificationCode

ABP version NuGet NuGet Download Discord online GitHub stars

An ABP module to generate and verify verification codes.

Installation

  1. Install the following NuGet packages. (see how)

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

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). And you can replace the DefaultIdentityVerificationCodeConfigurationProvider to customize the verification code generation rules.

Q&A

How to Change the Code Generation Strategy

You can instantiate a custom VerificationCodeConfiguration, 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.

About

An ABP module to generate and verify verification codes.

Resources

License

Stars

Watchers

Forks

Languages