GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - MarkoPapic/TwoFactorAuthentication: A C# library for 2-factor authentication.

BranchesTags
Open more actions menu

Latest commit

 

History

1 Commit
1 Commit

Folders and files

NameName
Last commit message
Last commit date
MarkoPapic.TwoFactorAuthentication
MarkoPapic.TwoFactorAuthentication
 
 
.gitignore
.gitignore
 
 
LICENSE.txt
LICENSE.txt
 
 
README.md
README.md
 
 
TwoFactorAuthentication.sln
TwoFactorAuthentication.sln
 
 

Repository files navigation

Two-Factor Authentication

NuGet Version

A C# library for TOTP (RFC 6238) 2-factor authentication.

Installation

Visual Studio Package Manager Console:

Install-Package MarkoPapic.TwoFactorAuthentication

dotnet CLI:

dotnet add package MarkoPapic.TwoFactorAuthentication

Usage

Authenticator app

To generate a new authenticator app key:

TwoFactorAuthenticationManager twoFactorAuthenticationManager = new TwoFactorAuthenticationManager();
string authenticatorKey = twoFactorAuthenticationManager.AuthenticatorApp.GenerateKey();

The authenticatorKey should be entered in an authenticator app. Then, to validate a code generated by an authenticator app:

bool isCodeValid = twoFactorAuthenticationManager.AuthenticatorApp.ValidateCode(key, code);

Where code it the code generated by an authenticator app.

Message based

To generate a TOTP to be sent to the user (via SMS, email...):

TwoFactorAuthenticationManager twoFactorAuthenticationManager = new TwoFactorAuthenticationManager();
string totp = twoFactorAuthenticationManager.Message.GenerateTotp(userKey);

Where userKey is a Base32 encoded string that is uniquely associated to this user. This key should be provided by you. Then, to validate the TOTP sent by the user:

bool isTotpValid = twoFactorAuthenticationManager.Message.ValidateCode(userKey, totp);

Where userKey is the same key you used to generate the TOTP, and totp is the TOTP code generated in the previous step.

Configuration

You can configure the following parameters:

  • MessageTotpDuration: The duration for which message-based TOTPs should be valid. Default is 300 seconds.
  • AuthenticatorTotpVarianceAllowed: Allows up to the specified adjacent intervals to be checked when validating authenticator app TOTPs. This can make up for delays caused by latency or clock missmatch. Default is 0.
  • MessageTotpVarianceAllowed: Allows up to the specified adjacent intervals to be checked when validating message-based TOTPs. This can make up for delays caused by latency or clock missmatch. Default is 0.

Example:

TwoFactorAuthenticationManager twoFactorAuthenticationManager = new TwoFactorAuthenticationManager(new TwoFactorAuthenticationOptions
    {
        MessageTotpDuration = 500,
        MessageTotpVarianceAllowed = 1,
        AuthenticatorTotpVarianceAllowed = 0
    });

Using .Net Core Dependency Injection

You can use the .NET Core middleware to register TwoFactorAuthenticationManager as a service available via .NET dependency injection:

public void ConfigureServices(IServiceCollection services)
{
   // ...

    services.AddTwoFactorAuthentication();

    services.Configure<TwoFactorAuthenticationOptions>(options =>
    {
        options.MessageTotpDuration = 500;
    });
    
    // ...
}

About

A C# library for 2-factor authentication.

Topics

2farfc-6238securitytotptwo-factor-authentication

Resources

Readme
MIT license
Activity

Stars

1 star

Watchers

1 watching

Forks

0 forks
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.