public static void Main()
int otpValidityInSeconds = 5;
var otp = MultiFactorAuthenticationHelper.GetFactorCode("ashu",otpValidityInSeconds,otpSize);
Console.WriteLine("Generated OTP is = "+ otp);
Console.WriteLine(string.Format("\nIt is valid for {0} seconds and is of {1} digits",otpValidityInSeconds,otpSize));
public static class MultiFactorAuthenticationHelper
public static string GetFactorCode(string otpKeyStr, int otpValidityInSeconds,int otpSize)
var otpKeyBytes = Base32Encoding.ToBytes(otpKeyStr);
var totp = new Totp(otpKeyBytes, step: otpValidityInSeconds, totpSize:otpSize);
var twoFactorCode = totp.ComputeTotp();