BrandonPotter / GoogleAuthenticator

Simple, easy to use server-side two-factor authentication library for .NET that works with Google Authenticator and Authy.
Apache License 2.0
369 stars 126 forks source link

GoogleAuthenticator

Simple, easy to use server-side two-factor authentication library for .NET that works with Google Authenticator

Build Status NuGet Status

Install-Package GoogleAuthenticator

Usage

Additional examples at Google.Authenticator.WinTest and Google.Authenticator.WebSample

key should be stored by your application for future authentication and shouldn't be regenerated for each request. The process of storing the private key is outside the scope of this library and is the responsibility of the application.

using Google.Authenticator;

string key;

TwoFactorAuthenticator tfa = new TwoFactorAuthenticator();
SetupCode setupInfo = tfa.GenerateSetupCode("Test Two Factor", "user@example.com", key, false, 3);

string qrCodeImageUrl = setupInfo.QrCodeSetupImageUrl;
string manualEntrySetupCode = setupInfo.ManualEntryKey;

imgQrCode.ImageUrl = qrCodeImageUrl;
lblManualSetupCode.Text = manualEntrySetupCode;

// verify
TwoFactorAuthenticator tfa = new TwoFactorAuthenticator();
bool result = tfa.ValidateTwoFactorPIN(key, txtCode.Text)

Update history

3.3.0

Added support for configuring the "time step". This is basically how often the code changes. The default used by most authenticator apps is 30 seconds, but some hardware devices use 60 seconds. You can now specify this in the constructor.

3.2.0

Added support for HMACSHA256 and HMACSHA512 as per the RFC spec. In testing it was found that several popular apps (such as Authy and Microsoft Authenticator) may not have support for these algorithms so care should be taken by the developer to ensure compatible apps are used.

3.1.1

Fixed an edge case where specifying an interval of 30 seconds to the Validate function would be treated as if you had passed in 0.

3.1.0

3.0.0

2.5.0

Now runs on .Net 6.0.
Technically the QR Coder library we rely on still does not fully support .Net 6.0 so it is possible there will be other niggling issues, but for now all tests pass for .Net 6.0 on both Windows and Linux.

Common Pitfalls