An ABP module to generate and verify verification codes.
-
Install the following NuGet packages. (see how)
- EasyAbp.Abp.VerificationCode
- (Optional) EasyAbp.Abp.VerificationCode.Identity
-
Add
DependsOn(typeof(AbpVerificationCodeXxxModule))
attribute to configure the module dependencies. (see how)
-
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.
-
Validate a verification code.
var verificationCodeManager = ServiceProvider.GetRequiredService<IVerificationCodeManager>(); var result = await verificationCodeManager.ValidateAsync( codeCacheKey: $"DangerousOperationPhoneVerification:{phoneNumber}", verificationCode: code, configuration: new VerificationCodeConfiguration());
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.
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"
Todo.