TurkishId.ModelBinder 4.0.0

dotnet add package TurkishId.ModelBinder --version 4.0.0
                    
NuGet\Install-Package TurkishId.ModelBinder -Version 4.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="TurkishId.ModelBinder" Version="4.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="TurkishId.ModelBinder" Version="4.0.0" />
                    
Directory.Packages.props
<PackageReference Include="TurkishId.ModelBinder" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add TurkishId.ModelBinder --version 4.0.0
                    
#r "nuget: TurkishId.ModelBinder, 4.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=TurkishId.ModelBinder&version=4.0.0
                    
Install TurkishId.ModelBinder as a Cake Addin
#tool nuget:?package=TurkishId.ModelBinder&version=4.0.0
                    
Install TurkishId.ModelBinder as a Cake Tool

NuGet Version Build status

TurkishId

Validator, model binder and generator tool for Turkish Republic's citizen ID numbers. I've decided to give this a shot last night while I was waiting for a download. And Turkish ID numbers were really popular in Turkish social media last week. The Id number is called "T.C. Kimlik No" (Turkish Republic Identity Number). I decided to use English translation to allow easier handling in international projects.

Usage

I wanted the TurkishIdNumber representation to be used anywhere in the code as a value to pass around when using id's allowing to assume an instance is already validated saving you from redundant checks.

When you want to use it as a representation of an ID number:

using TurkishId;
var id = new TurkishIdNumber("12345678901");
// throws ArgumentException when invalid parameter is passed

or if you just want to validate it:

using TurkishId;
bool reallyValid = TurkishIdNumber.IsValid("12345678901");

You can also use the classic TryParse:

using TurkishId;
if (TurkishIdNumber.TryParse(value, out var id))
{
    // ...
}

or with a nullable return value which can be used with pattern matching:

using TurkishId;
if (TurkishIdNumber.TryParse(value) is TurkishIdNumber id))
{
    // ...
}

NuGet package is here: https://www.nuget.org/packages/TurkishId/

TurkishId.ModelBinder

There is also a model binder package that you can install at https://www.nuget.org/packages/TurkishId.ModelBinder. It's a plug'n'play model binder which lets you to use TurkishIdNumber class directly in your model declarations.

It's not part of the original package because you may not want to have whole MVC as a dependency.

To set it up in an ASP.NET Core project, use this syntax in your ConfigureServices() method in your Startup class:

services.AddMvc(options =>
{
  options.ModelBinderProviders.Insert(0, new TurkishIdModelBinderProvider());
});

and you're done. If you'd like to use it in your Razor Pages app use AddMvcOptions instead:

services.AddRazorPages()
  .AddMvcOptions(options => options.ModelBinderProviders.Insert(0, new TurkishIdModelBinderProvider()));

or, alternatively, if you only use controllers, you can add it to your AddControllers options:

services.AddControllers(options =>
{
  options.ModelBinderProviders.Insert(0, new TurkishIdModelBinderProvider());
});

The model binder package will use ASP.NET Core's model binding message providers. You can now localize them like how you do any other model binder.

Performance

This is probably one of the fastest implementations on .NET. I didn't grind so much on performance but it can easily handle millions of validations per second on my Core i7.

Algorithm

Turkish Republic's ID structure and verification is simple. It's an eleven digit number. If we name each digit as d(n) where leftmost digit is called d1 and the rightmost d11, a given ID is valid if:

d1 > 0

and

n = (d1 + d3 + d5 + d7 + d9) * 7 - (d2 + d4 + d6 + d8)

if n < 0 then n = n + 10

d10 = n mod 10

and

d11 = sum(d1..d10) mod 10

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
4.0.0 0 5/9/2025
3.3.0 118 11/13/2024
3.2.0 464 11/11/2022
3.1.0 487 4/10/2022
3.0.1 475 12/16/2020
3.0.0 409 12/14/2020

# Changes
- Target .NET 8.0