SkillAgentSearch skills...

ActiveLogin.Identity

Parsing and validation of Swedish identities such as Personal Identity Number (svenskt personnummer) in .NET.

Install / Use

/learn @ActiveLogin/ActiveLogin.Identity

README

ActiveLogin.Identity

License: MIT Build Status Build Status Discussion Twitter Follow

ActiveLogin.Identity provides parsing and validation of Swedish identities such as Personal Identity Number (svenskt personnummer) and Coordination Number (samordningsnummer). Built on NET Standard and packaged as NuGet-packages they are easy to install and use on multiple platforms.

Features

  • :id: .NET parser for Swedish Personal Identity Number (Svenskt personnummer)
  • :penguin: Cross platform: Targets .NET Standard 2.0
  • :heavy_check_mark: Strong named
  • :lock: GDPR Compliant
  • :calendar: Extracts metadata such as date of birth and gender
  • :large_blue_diamond: Written in F# and C# and works great with VB.NET as well
  • :white_check_mark: Well tested

Table of contents

Project & Packages overview

CI-builds from master of all packages are available in our Azure DevOps Artifacts feed.

| Project | Description | NuGet | Downloads | | ------- | ----------- | ----- | --------- | | ActiveLogin.Identity.Swedish | .NET classes handling Personal Identity Number | NuGet | NuGet | | ActiveLogin.Identity.Swedish.AspNetCore | Validation attributes for ASP.NET Core. | NuGet | NuGet | | ActiveLogin.Identity.Swedish.TestData | Provides Swedish Identity Numbers test data. | NuGet | NuGet |

Getting started

1. Install the NuGet package

ActiveLogin.Identity is distributed as packages on NuGet, install using the tool of your choice, for example dotnet cli:

dotnet add package ActiveLogin.Identity.Swedish

2. Use the library in your C# project

PersonalIdentityNumber

PersonalIdentityNumber provides parsing methods such as PersonalIdentityNumber.Parse() and PersonalIdentityNumber.TryParse() that can be used like this:

var rawPersonalIdentityNumber = "990807-2391";
if (PersonalIdentityNumber.TryParse(rawPersonalIdentityNumber, out var personalIdentityNumber))
{
    Console.WriteLine("PersonalIdentityNumber");
    Console.WriteLine(" .ToString(): {0}", personalIdentityNumber.ToString());
    Console.WriteLine(" .To10DigitString(): {0}", personalIdentityNumber.To10DigitString());
    Console.WriteLine(" .To12DigitString(): {0}", personalIdentityNumber.To12DigitString());

    Console.WriteLine(" .GetDateOfBirthHint(): {0}", personalIdentityNumber.GetDateOfBirthHint().ToShortDateString());
    Console.WriteLine(" .GetAgeHint(): {0}", personalIdentityNumber.GetAgeHint().ToString());

    Console.WriteLine(" .GetGenderHint(): {0}", personalIdentityNumber.GetGenderHint().ToString());
    
    // IsTestNumber is an extension method from the package ActiveLogin.Identity.Swedish.TestData
    Console.WriteLine(" .IsTestNumber(): {0}", personalIdentityNumber.IsTestNumber().ToString());
}
else
{
    Console.Error.WriteLine("Unable to parse the input as a PersonalIdentityNumber.");
}

The code above would output (as of 2018-07-23):

PersonalIdentityNumber
 .ToString(): 199908072391
 .To10DigitString(): 990807-2391
 .To12DigitString(): 199908072391
 .GetDateOfBirthHint(): 1999-08-07
 .GetAgeHint(): 18
 .GetGenderHint(): Male
 .IsTestNumber(): True

StrictMode

The library can be configured to use different levels 'strictness' when parsing identity numbers. The different levels are:

  • Off
  • Ten Digits
  • Twelve Digits
  • Ten or Twelve Digits

By default 'Ten or Twelve Digits' is used but it can be overridden when calling Parse and TryParse, e.g.:

// this would fail since the input is not a 12 digit number.
PersonalIdentityNumber.Parse("990807-2391", StrictMode.TwelveDigits); 

For more information regarding StrictMode, see the FAQ.

Hints

Some data, such as DateOfBirth, Age and Gender can't be guaranteed to reflect the truth due to the limited quantity of personal identity numbers per day. Therefore they are exposed as extension methods in the C# api and are suffixed with Hint to reflect this. They are also placed in a separate namespace ActiveLogin.Identity.Swedish.Extensions. In the F# api these functions are available in the ActiveLogin.Identity.Swedish.PersonalIdentityNumber.Hints module.

ASP.NET Core MVC

If used to validate input in an ASP.NET Core MVC project, the PersonalIdentityNumberAttribute can be used like this:

public class SampleDataModel
{
    [PersonalIdentityNumber]
    public string PersonalIdentityNumber { get; set; }
}

The PersonalIdentityNumber attribute is available through a separate package:

dotnet add package ActiveLogin.Identity.Swedish.AspNetCore

CoordinationNumber

CoordinationNumber provides parsing methods such as CoordinationNumber.Parse() and CoordinationNumber.TryParse() that can be used like this:

var rawCoordinationNumber = "680164-2395";
if (CoordinationNumber.TryParse(rawCoordinationNumber, out var coordinationNumber))
{
    Console.WriteLine("CoordinationNumber");
    Console.WriteLine(" .ToString(): {0}", coordinationNumber.ToString());
    Console.WriteLine(" .To10DigitString(): {0}", coordinationNumber.To10DigitString());
    Console.WriteLine(" .To12DigitString(): {0}", coordinationNumber.To12DigitString());
    Console.WriteLine(" .RealDay: {0}", coordinationNumber.RealDay;

    Console.WriteLine(" .GetDateOfBirthHint(): {0}", coordinationNumber.GetDateOfBirthHint().ToShortDateString());
    Console.WriteLine(" .GetAgeHint(): {0}", coordinationNumber.GetAgeHint().ToString());

    Console.WriteLine(" .GetGenderHint(): {0}", coordinationNumber.GetGenderHint().ToString());
    
    // IsTestNumber is an extension method from the package ActiveLogin.Identity.Swedish.TestData
    Console.WriteLine(" .IsTestNumber(): {0}", coordinationNumber.IsTestNumber().ToString());
}
else
{
    Console.Error.WriteLine("Unable to parse the input as a CoordinationNumber.");
}

The code above would output (as of 2018-07-23):

CoordinationNumber
 .ToString(): 199908072391
 .To10DigitString(): 990807-2391
 .To12DigitString(): 199908072391
 .RealDay: 7
 .GetDateOfBirthHint(): 1999-08-07
 .GetAgeHint(): 18
 .GetGenderHint(): Male
 .IsTestNumber(): True

Hints

Some data, such as DateOfBirth, Age and Gender can't be guaranteed to reflect the truth due to the limited quantity of personal identity numbers per day. Therefore they are exposed as extension methods in the C# api and are suffixed with Hint to reflect this. They are also placed in a separate namespace `ActiveLogin.Identity.Swedi

View on GitHub
GitHub Stars57
CategoryDevelopment
Updated11mo ago
Forks10

Languages

F#

Security Score

92/100

Audited on Apr 8, 2025

No findings