MasLazu.AspNet.Verification
1.0.0-preview.6
dotnet add package MasLazu.AspNet.Verification --version 1.0.0-preview.6
NuGet\Install-Package MasLazu.AspNet.Verification -Version 1.0.0-preview.6
<PackageReference Include="MasLazu.AspNet.Verification" Version="1.0.0-preview.6" />
<PackageVersion Include="MasLazu.AspNet.Verification" Version="1.0.0-preview.6" />
<PackageReference Include="MasLazu.AspNet.Verification" />
paket add MasLazu.AspNet.Verification --version 1.0.0-preview.6
#r "nuget: MasLazu.AspNet.Verification, 1.0.0-preview.6"
#:package MasLazu.AspNet.Verification@1.0.0-preview.6
#addin nuget:?package=MasLazu.AspNet.Verification&version=1.0.0-preview.6&prerelease
#tool nuget:?package=MasLazu.AspNet.Verification&version=1.0.0-preview.6&prerelease
MasLazu.AspNet.Verification
The Application layer of the MasLazu ASP.NET Verification system. This project contains the core business logic, services, validators, and utilities for handling verification workflows.
๐ Overview
This is the application layer that implements the verification business logic using Clean Architecture principles. It provides services for creating, managing, and validating verification codes across different channels (email, SMS, etc.).
๐๏ธ Architecture
This project follows the Application layer pattern in Clean Architecture:
- Services: Business logic implementation
- Validators: Request validation using FluentValidation
- Extensions: Dependency injection utilities
- Utils: Property mapping and expression building utilities
๐ฆ Dependencies
Project References
MasLazu.AspNet.Verification.Abstraction- Core interfaces and modelsMasLazu.AspNet.Verification.Domain- Domain entities
Package References
MasLazu.AspNet.Framework.Application- Base application frameworkMasLazu.AspNet.EmailSender.Abstraction- Email sending abstractionMassTransit- Message bus for eventsFluentValidation- Validation frameworkMapster- Object mapping
๐ Features
Core Services
VerificationService
The main service for verification operations:
public class VerificationService : CrudService<Domain.Entities.Verification, VerificationDto, CreateVerificationRequest, UpdateVerificationRequest>, IVerificationService
Key Methods:
CreateVerificationAsync()- Creates new verification codesSendVerificationAsync()- Sends verification codes via emailVerifyAsync()- Validates and marks codes as verifiedIsCodeValidAsync()- Checks code validityGetByCodeAsync()- Retrieves verification by code
Email Integration:
- Uses
IEmailSenderfor sending emails - Supports HTML templates with
IHtmlRenderer - Publishes
VerificationCompletedEventvia MassTransit
VerificationPurposeService
CRUD service for managing verification purposes:
public class VerificationPurpose : CrudService<VerificationPurpose, VerificationPurposeDto, CreateVerificationPurposeRequest, UpdateVerificationPurposeRequest>, IVerificationPurposeService
Key Methods:
CreateIfNotExistsAsync()- Creates purpose if it doesn't exist
Validation System
Comprehensive request validation using FluentValidation:
Available Validators
CreateVerificationRequestValidatorUpdateVerificationRequestValidatorSendVerificationRequestValidatorVerifyCodeRequestValidatorCreateVerificationPurposeRequestValidatorUpdateVerificationPurposeRequestValidator
Example Validation Rules
public class CreateVerificationRequestValidator : AbstractValidator<CreateVerificationRequest>
{
RuleFor(x => x.UserId).NotEmpty();
RuleFor(x => x.Channel).IsInEnum();
RuleFor(x => x.Destination).NotEmpty().EmailAddress()
.When(x => x.Channel == VerificationChannel.Email);
RuleFor(x => x.ExpiresAt).GreaterThan(DateTimeOffset.UtcNow)
.When(x => x.ExpiresAt.HasValue);
}
Property Mapping System
Dynamic property mapping for sorting and filtering:
VerificationEntityPropertyMap
public class VerificationEntityPropertyMap : IEntityPropertyMap<Domain.Entities.Verification>
{
private readonly Dictionary<string, Expression<Func<Domain.Entities.Verification, object>>> _map = new()
{
{ "id", v => v.Id },
{ "userId", v => v.UserId },
{ "channel", v => v.Channel },
{ "status", v => v.Status },
// ... more mappings
};
}
Dependency Injection Extensions
VerificationApplicationUtilExtension
Utility for registering property maps and expression builders:
public static class VerificationApplicationUtilExtension
{
public static IServiceCollection AddVerificationApplicationUtils(this IServiceCollection services)
{
RegisterPropertyMapsAndExpressionBuilders(services);
return services;
}
}
๐ง Configuration
Service Registration
// Register application utilities
services.AddVerificationApplicationUtils();
// Register email services
services.AddScoped<IEmailSender, YourEmailSenderImplementation>();
services.AddScoped<IHtmlRenderer, YourHtmlRendererImplementation>();
// Register MassTransit for events
services.AddMassTransit(config => {
// Configure message bus
});
Email Configuration
The service expects these dependencies to be registered:
IEmailSender- For sending emailsIHtmlRenderer- For rendering HTML templatesIPublishEndpoint- For publishing events
๐ Usage Examples
Creating a Verification
var verificationService = serviceProvider.GetRequiredService<IVerificationService>();
var request = new CreateVerificationRequest(
UserId: Guid.NewGuid(),
Channel: VerificationChannel.Email,
Destination: "user@example.com",
VerificationPurposeCode: "registration",
ExpiresAt: DateTimeOffset.UtcNow.AddMinutes(15)
);
var verification = await verificationService.CreateVerificationAsync(userId, request);
Sending Verification Email
var sendRequest = new SendVerificationRequest(
UserId: userId,
Destination: "user@example.com",
PurposeCode: "registration"
);
var verification = await verificationService.SendVerificationAsync(userId, sendRequest);
Verifying a Code
var isValid = await verificationService.IsCodeValidAsync(userId, code);
if (isValid)
{
var result = await verificationService.VerifyAsync(code);
// Handle successful verification
}
Email Template Usage
private async Task SendEmailVerificationAsync(VerificationDto verification)
{
EmailMessage email = new EmailMessageBuilder()
.From("security@yourapp.com", "Your App Security")
.To(verification.Destination)
.Subject("๐ Verify Your Account")
.RenderOptions(new EmailRenderOptions
{
Theme = "VerificationCode",
CompanyName = "Your Company",
PrimaryColor = "#28a745"
})
.Model(new
{
VerificationCode = verification.VerificationCode,
UserName = "User",
ExpiryMinutes = 15
})
.Build();
await _emailSender.SendEmailAsync(email, _htmlRenderer);
}
๐ Event System
The service publishes events through MassTransit:
VerificationCompletedEvent
Published when a verification is successfully completed:
var verificationEvent = new VerificationCompletedEvent
{
VerificationId = verification.Id,
UserId = verification.UserId,
Email = verification.Destination,
PurposeCode = verification.VerificationPurposeCode,
CompletedAt = DateTime.UtcNow,
IsSuccessful = true
};
await _publishEndpoint.Publish(verificationEvent);
๐งช Testing
Unit Testing Services
[Fact]
public async Task CreateVerificationAsync_ShouldCreateVerification()
{
// Arrange
var mockRepo = new Mock<IRepository<Domain.Entities.Verification>>();
var service = new VerificationService(/* dependencies */);
// Act
var result = await service.CreateVerificationAsync(userId, request);
// Assert
Assert.NotNull(result);
Assert.Equal(VerificationStatus.Pending, result.Status);
}
Testing Validators
[Fact]
public void CreateVerificationRequestValidator_ShouldValidateEmail()
{
var validator = new CreateVerificationRequestValidator();
var request = new CreateVerificationRequest
{
Channel = VerificationChannel.Email,
Destination = "invalid-email"
};
var result = validator.Validate(request);
Assert.False(result.IsValid);
Assert.Contains(result.Errors, e => e.PropertyName == "Destination");
}
๐ Extensibility
Adding Custom Validators
public class CustomVerificationValidator : AbstractValidator<CreateVerificationRequest>
{
public CustomVerificationValidator()
{
RuleFor(x => x.CustomField)
.NotEmpty()
.Must(BeValidCustomField);
}
private bool BeValidCustomField(string field)
{
// Custom validation logic
return true;
}
}
Extending Property Maps
public class ExtendedVerificationPropertyMap : VerificationEntityPropertyMap
{
public ExtendedVerificationPropertyMap()
{
// Add additional property mappings
_map.Add("customProperty", v => v.CustomProperty);
}
}
Custom Email Templates
Implement IHtmlRenderer for custom template rendering:
public class CustomHtmlRenderer : IHtmlRenderer
{
public Task<string> RenderAsync(string template, object model)
{
// Custom rendering logic
return Task.FromResult("Rendered HTML");
}
}
๐ ๏ธ Development
Code Structure
src/MasLazu.AspNet.Verification/
โโโ Extensions/ # DI extensions
โโโ Services/ # Business logic services
โโโ Utils/ # Property maps and utilities
โโโ Validators/ # Request validators
โโโ MasLazu.AspNet.Verification.csproj
Best Practices
- Use dependency injection for all services
- Implement comprehensive validation
- Follow async/await patterns
- Use meaningful exception messages
- Document public APIs with XML comments
๐ค Contributing
- Maintain Clean Architecture principles
- Add unit tests for new functionality
- Update validators for new request types
- Follow naming conventions
- Keep services focused on single responsibilities
๐ License
Part of the MasLazu ASP.NET framework ecosystem.</content> <parameter name="filePath">/home/mfaziz/projects/cs/MasLazu.AspNet.Verification/src/MasLazu.AspNet.Verification/README.md
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net9.0
- MasLazu.AspNet.EmailSender.Abstraction (>= 1.0.0-preview.1)
- MasLazu.AspNet.Framework.Application (>= 1.0.0-preview.15)
- MasLazu.AspNet.Verification.Abstraction (>= 1.0.0-preview.6)
- MasLazu.AspNet.Verification.Domain (>= 1.0.0-preview.6)
- MassTransit (>= 8.5.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.0.0-preview.6 | 143 | 10/2/2025 |
| 1.0.0-preview.5 | 145 | 10/1/2025 |
| 1.0.0-preview.4 | 146 | 9/29/2025 |
| 1.0.0-preview.3 | 140 | 9/29/2025 |
| 1.0.0-preview.2 | 117 | 9/28/2025 |
| 1.0.0-preview.1 | 270 | 9/18/2025 |