Nafath.Net is a modern .NET SDK for integrating with the Nafath authentication system provided by SDAIA.
Nafath.Net is a clean and modern .NET SDK for integrating with the official Nafath authentication system provided by SDAIA (Saudi Arabia).
Requirement | Version |
---|---|
.NET SDK | 8.0+ |
Supported Platforms | .NET 8, 7 |
IDE | VS Code / Visual Studio 2022+ |
Until it's published to NuGet, add a local project reference:
dotnet add reference ../Nafath.Net/Nafath.Net.csproj
Once available on NuGet:
dotnet add package Nafath.Net
builder.Services.AddNafath(options =>
{
options.BaseUrl = "https://auth.nafath.sa";
options.ClientId = "your-client-id";
options.ClientSecret = "your-client-secret";
});
Use User Secrets, environment variables, or Azure Key Vault for secrets.
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddNafath(options =>
{
options.BaseUrl = "https://auth.nafath.sa";
options.ClientId = builder.Configuration["Nafath:ClientId"];
options.ClientSecret = builder.Configuration["Nafath:ClientSecret"];
});
var app = builder.Build();
app.MapPost("/nafath/start", async (INafathService nafath, string idNumber, string requestId) =>
{
var result = await nafath.StartSessionAsync(idNumber, requestId);
return Results.Ok(result);
});
app.MapGet("/nafath/verify/{transactionId}", async (INafathService nafath, string transactionId) =>
{
var result = await nafath.VerifyStatusAsync(transactionId);
return Results.Ok(result);
});
app.Run();
Initiates an authentication session.
Checks if the user accepted/rejected the request.
Both methods return strongly typed models with status and error information.
Robust built-in error handling with structured logging:
- Network issues (timeouts, 500s)
- Unexpected API responses
- Logs both outbound payload and response content
Add breakpoints inside NafathService.cs
to debug calls directly.
public class NafathServiceTests
{
[Fact]
public async Task StartSessionAsync_Returns_ValidResponse()
{
// Arrange
var httpClient = new HttpClient(new MockHttpMessageHandler());
var options = Options.Create(new NafathOptions { BaseUrl = "https://fake.api" });
var logger = new LoggerFactory().CreateLogger<NafathService>();
var service = new NafathService(httpClient, options, logger);
// Act
var result = await service.StartSessionAsync("1234567890", "test-req");
// Assert
Assert.NotNull(result);
Assert.True(string.IsNullOrEmpty(result.Error));
}
}
🔸 You can mock HttpMessageHandler
using Moq
or RichardSzalay.MockHttp
.
Or use GitHub Actions to automate this (see .github/workflows/ci.yml
example).
We welcome contributions! Please:
- Submit clear PRs
- Open issues for feature requests or bugs
- Follow .NET and API design best practices