google authenticator with asp.net core
Asked Answered
C

1

7

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.

Clough answered 13/5, 2017 at 17:57 Comment(1)
I found this post quite helpful: chsakell.com/2019/08/18/…Submission
S
8

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);
Sinapism answered 10/8, 2017 at 14:15 Comment(2)
Hi, thank you for the suggestion. The manual setup key works, but the QR code does not. I got very long string of QR code like more than 2000 characters that could not interpret in the browser. The QR code string looks like : ""data:image/png;base64,iVBORw0KGgoAAAANSU....". Do you know any way to decode it into simple QR code?Headship
That string is the image data in base64 format. To display it in a browser, have the string show in a <img> tag like so: <img src="data:image/png;base64,iVBORw0KGgoAAAANSU....">Irritating

© 2022 - 2024 — McMap. All rights reserved.