TwoStepsAuthenticator 1.3.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package TwoStepsAuthenticator --version 1.3.0                
NuGet\Install-Package TwoStepsAuthenticator -Version 1.3.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="TwoStepsAuthenticator" Version="1.3.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add TwoStepsAuthenticator --version 1.3.0                
#r "nuget: TwoStepsAuthenticator, 1.3.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.
// Install TwoStepsAuthenticator as a Cake Addin
#addin nuget:?package=TwoStepsAuthenticator&version=1.3.0

// Install TwoStepsAuthenticator as a Cake Tool
#tool nuget:?package=TwoStepsAuthenticator&version=1.3.0                

TwoStepsAuthenticator

Build status NuGet

.net implementation of the TOTP: Time-Based One-Time Password Algorithm and HOTP: HMAC-Based One-Time Password Algorithm RFC 6238 http://tools.ietf.org/html/rfc6238 RFC 4226 http://tools.ietf.org/html/rfc4226

Compatible with Microsoft Authenticator for Windows Phone, and Google Authenticator for Android and iPhone.

You can use this library as well for a client application (if you want to create your own authenticator) or for a server application (add two-step authentication on your asp.net website)

Install using Nuget

To install TwoStepsAuthenticator, run the following command in the Package Manager Console

Install-Package TwoStepsAuthenticator

TOTP

Client usage

For a client application, you need to save the secret key for your user. <br/> Then, you only have to call the method GetCode(string) :

var secret = user.secretAuthToken;
var authenticator = new TwoStepsAuthenticator.TimeAuthenticator();
var code = authenticator.GetCode(secret);

Server usage

On a server application, you will have to generate a secret key, and share it with the user, who will have to enter it in his own authenticator app.

var key = TwoStepsAuthenticator.Authenticator.GenerateKey();

When the user will login, he will have to give you the code generated by his authenticator.<br/> You can check if the code is correct with the method CheckCode(string secret, string code).<br/> If the code is incorrect, don't log him.

var secret = user.secretAuthToken;
var code = Request.Form["code"];
var authenticator = new TwoStepsAuthenticator.TimeAuthenticator();
bool isok = authenticator.CheckCode(secret, code);

Used codes manager

Every code should only be used once. To prevent repeated use of a code a IUsedCodesManager interface is provided.<br>

A default implementation is provided : used codes are kept in memory for 5 minutes (long enough for codes to become invalid)

You can define how the used codes are stored, for example if you want to handle persistence (database storage), or if you have multiple webservers.<br/> You have to implement the 2 methods of the IUsedCodesManager :

void AddCode(ulong challenge, string code, object user);
bool IsCodeUsed(ulong challenge, string code, object user);

The user class must implement correctly the GetHashCode and Equals methods, because they are used to check if a specific user has used each code.

When you create a new Authenticator, add the instance of your IUsedCodesManager as the first param

var usedCodeManager = new CustomUsedCodeManager();
var authenticator = new TwoStepsAuthenticator.TimeAuthenticator(usedCodeManager);

And when you check if the code is ok, you need to add the user object to the CheckCode method

bool isok = authenticator.CheckCode(secret, code, user);

HOTP

With the HOTP authenticator, the user has an ordered list of codes, and he uses them one by one. When a code is used, all the previous codes are disabled.

On a server application, you will generate a secret key, and you need to store for every user its secret key and the index of the last code used.

var key = TwoStepsAuthenticator.Authenticator.GenerateKey();

When the user sends his code, you need to check if it is valid, and is after the last code used. The CheckCode method will check the 10 folloing codes, and return the id of the code used.

var secret = user.secretAuthToken;
var lastCodeUsed = user.lastCodeUsed;
var code = Request.Form["code"];
var authenticator = new TwoStepsAuthenticator.CounterAuthenticator();
ulong newCodeUsed;
bool isok = authenticator.CheckCode(secret, code, lastCodeUsed, out newCodeUsed);

if (isOk) {
    user.lastCodeUsed = newCodeUsed;
}

If you want to generate the codes, you can use the method GetCode(string secret, ulong counter)

var nextCodes = new List<strig>();
var secret = user.secretAuthToken;
var lastCodeUsed = user.lastCodeUsed;
var authenticator = new TwoStepsAuthenticator.CounterAuthenticator();
for (int i = lastCodeUsed + 1 ; i < lastCodeUsed + 100 ; i++){
    nextCodes.Add(authenticator.GetCode(secret, i));
}
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
.NET Core netcoreapp1.0 was computed.  netcoreapp1.1 was computed.  netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard1.6 is compatible.  netstandard2.0 was computed.  netstandard2.1 was computed. 
.NET Framework net45 is compatible.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen30 was computed.  tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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 (1)

Showing the top 1 popular GitHub repositories that depend on TwoStepsAuthenticator:

Repository Stars
Seuntjie900/DiceBot
Flexible and feature rich betting bot for a variety of crypto-currency dice sites
Version Downloads Last updated
1.4.1 281,982 11/30/2019
1.4.0 2,480 9/28/2019
1.3.2 32,013 5/9/2019
1.3.1 14,240 8/28/2018
1.3.0 10,881 3/11/2018
1.2.0 34,302 8/28/2016
1.1.0 2,249 6/29/2016
1.0.1.1 1,912 11/6/2015