ActiveLogin.Identity provides parsing and validation of Swedish identities such as Personal Identity Number (svenskt personnummer). Built on NET Standard and packaged as NuGet-packages they are easy to install and use on multiple platforms.
- 🆔 .NET parser for Swedish Personal Identity Number (Svenskt personnummer)
- 🐧 Cross platform: Targets .NET Standard 2.0 and .NET Framework 4.6.1
- ✔️ Strong named
- 🔒 GDPR Compliant
- 📆 Extracts metadata such as date of birth and gender
- 🔷 Written in F# and C# and works great with VB.NET as well
- ✅ Well tested
| Project | Description | NuGet |
|---|---|---|
| ActiveLogin.Identity.Swedish | .NET classes handling Personal Identity Number | |
| ActiveLogin.Identity.Swedish.AspNetCore | Validation attributes for ASP.NET Core. |
ActiveLogin.Identity is distributed as packages on NuGet, install using the tool of your choice, for example dotnet cli:
dotnet add package ActiveLogin.Identity.Swedishor
dotnet add package ActiveLogin.Identity.Swedish.AspNetCoreSwedishPersonalIdentityNumber provides parsing methods such as SwedishPersonalIdentityNumber.Parse() and SwedishPersonalIdentityNumber.TryParse() that can be used like this:
var rawPersonalIdentityNumber = "990807-2391";
if (SwedishPersonalIdentityNumber.TryParse(rawPersonalIdentityNumber, out var personalIdentityNumber))
{
Console.WriteLine("SwedishPersonalIdentityNumber");
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());
}
else
{
Console.Error.WriteLine("Unable to parse the input as a SwedishPersonalIdentityNumber.");
}The code above would output (as of 2018-07-23):
SwedishPersonalIdentityNumber
.ToString(): 199908072391
.To10DigitString(): 990807-2391
.To12DigitString(): 199908072391
.GetDateOfBirthHint(): 1999-08-07
.GetAgeHint(): 18
.GetGenderHint(): Male
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.FSharp.SwedishPersonalIdentityNumber.Hints module.
If used to validate input in an ASP.NET Core MVC project, the SwedishPersonalIdentityNumberAttribute can be used like this:
public class SampleDataModel
{
[SwedishPersonalIdentityNumber]
public string SwedishPersonalIdentityNumber { get; set; }
}open ActiveLogin.Swedish.Identity.FSharp
The SwedishPersonalIdentityNumber-module provides functions for parsing, creating and converting a SwedishPersonalIdentityNumber to its 10- or 12-digit string representation.
"990807-2391"
|> SwedishPersonalIdentityNumber.parse
|> function
| Ok pin ->
printfn "%A" pin
pin |> SwedishPersonalIdentityNumber.to10DigitString |> printfn "to10DigitString: %s"
pin |> SwedishPersonalIdentityNumber.to12DigitString |> printfn "to12DigitString: %s"
pin |> SwedishPersonalIdentityNumber.Hints.getDateOfBirthHint |> (fun date -> date.ToShortDateString() |> printfn "getDateOfBirthHint: %s")
pin |> SwedishPersonalIdentityNumber.Hints.getAgeHint |> printfn "getAgeHint: %i"
pin |> SwedishPersonalIdentityNumber.Hints.getGenderHint |> printfn "getGenderHint: %A"
| Error e -> printfn "Not a valid Swedish personal identity number. Error %A" e The code above would output (as of 2018-07-23):
{Year = Year 1999;
Month = Month 8;
Day = Day 7;
BirthNumber = BirthNumber 239;
Checksum = Checksum 1;}
to10DigitString: 990807-2391
to12DigitString: 199908072391
getDateOfBirthHint: 1999-08-07
getAgeHint: 18
getGenderHint: Male
For more usecases, samples and inspiration; feel free to browse our unit tests and samples:
- C# ConsoleSample
- F# ConsoleSample
- ActiveLogin.Identity.Swedish.Test
- ActiveLogin.Identity.Swedish.AspNetCore.Test
It will try cleaning away any invalid chars, while still preserving digits and + when that applies. The "standard" ways of input would be in any of these formats:
- YYMMDD-BBBC
- YYMMDD+BBBC
- YYMMDDBBBC
- YYYYMMDDBBBC
But it also supports other variations such as:
- YYMMDD BBBC
- YYYYMMDD-BBBC
- YYYYMMDD BBBC
- YYYY MM DD BBB C
- YY-MM-DD-BBBC
And basically anything else that can be cleaned and parsed :)
- YY: Year
- MM: Month
- DD: Day
- BBB: Birth number
- C: Checksum
The implementation is primarily based on the definition defined in Swedish Law:
But when there have been ambiguities we have also read more info and samples from these links from Swedish authorities:
Worth noticing is that the date part is not guaranteed to be the exact date you were born, but can be changed for another date within the same month.
The Swedish word "personnummer" is translated into "personal identity number" by Skatteverket and that's the translation we decided on using as it's used in official documents.
Unforentunately the term "social security number" or SSN is often used even for a swedish personal identity number, even though that is misleading as a SSN is something used in the United States and should not be mixed up with a PIN.
To comply with GDPR and not no expose any real PINs, we are using the official test data for Swedish Personal Identity Numbers provided by Skatteverket.
Some forms of a Swedish Personal Identity Number depends of the age of the person it represents.
The "-" will be replaced with a "+" on January 1st the year a person turns 100 years old. Therefore these methods (.To10DigitStringInSpecificYear(...), .ParseInSpecificYear(...), .TryParseInSpecificYear(...)) exists to define at what year the the data should be represented or parsed.
Useful for parsing old data or printing data for the future.
Integrating your systems with market leading authentication services.
Active Login is an Open Source project built on .NET Standard that makes it easy to integrate with leading Swedish authentication services like BankID.
It also provide examples of how to use it with the popular OpenID Connect & OAuth 2.0 Framework IdentityServer and provides a template for hosting the solution in Microsoft Azure. In addition, Active Login also contain convenient modules that help you work with and handle validation of Swedish Personal Identity Number (svenskt personnummer).
We are very open to community contributions to Active Login. You'll need a basic understanding of Git and GitHub to get started. The easiest way to contribute is to open an issue and start a discussion. If you make code changes, submit a pull request with the changes and a description. Don’t forget to always provide tests that cover the code changes.
Active Login is licensed under the very permissive MIT license for you to be able to use it in commercial or non-commercial applications without many restrictions.
Active Login is built on or uses the following great open source products: