Reputation: 2071
Are there any sample implementation for google authenticator as two factor authentication implementation in addition to sms and email?
Found one sample. Sample Google Authenticator using asp.net
But there are lots of changes while using it with asp.net core.
Upvotes: 7
Views: 6775
Reputation: 526
You can use AspNetCore.Totp. https://github.com/damirkusar/AspNetCore.Totp
It works exactly like GoogleAuthenticator, have a look at the Tests project for the implementation (really simple).
You just have a couple of lines to write to get the qurcode and to validate the pin code:
using AspNetCore.Totp;
...
// To generate the qrcode/setup key
var totpSetupGenerator = new TotpSetupGenerator();
var totpSetup = totpSetupGenerator.Generate("You app name here", "The username", "YourSuperSecretKeyHere", 300, 300);
string qrCodeImageUrl = totpSetup.QrCodeImage;
string manualEntrySetupCode = totpSetup.ManualSetupKey;
// To validate the pin after user input (where pin is an int variable)
var totpGenerator = new TotpGenerator();
var totpValidator = new TotpValidator(totpGenerator);
bool isCorrectPIN = totpValidator.Validate("YourSuperSecretKeyHere", pin);
Upvotes: 8