Google Authenticator available as a public service?

前端 未结 10 1264
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-04 04:24

Is there public API for using the Google Authenticator (two factor authentication) on self-running (e.g. LAMP stack) web apps?

10条回答
  •  -上瘾入骨i
    2020-12-04 05:16

    For C# user, run this simple Console App to understand how to verify the one time token code. Note that we need to install library Otp.Net from Nuget package first.

    static string secretKey = "JBSWY3DPEHPK3PXP"; //add this key to your Google Authenticator app  
    
    private static void Main(string[] args)
    {
            var bytes = Base32Encoding.ToBytes(secretKey);
    
            var totp = new Totp(bytes);
    
            while (true)
            {
                Console.Write("Enter your code from Google Authenticator app: ");
                string userCode = Console.ReadLine();
    
                //Generate one time token code
                string tokenInApp = totp.ComputeTotp();
                int remainingSeconds = totp.RemainingSeconds();
    
                if (userCode.Equals(tokenInApp)
                    && remainingSeconds > 0)
                {
                    Console.WriteLine("Success!");
                }
                else
                {
                    Console.WriteLine("Failed. Try again!");
                }
            }
    }
    

提交回复
热议问题