Пример #1
0
        public TwoFactorAuthInfo GoogleAuthenticatorSetupCode()
        {
            var tfa              = new TwoFactorAuthenticator();
            var user             = Security.CurrentUser;
            var accountSecretKey = Guid.NewGuid().ToString();
            var setupInfo        = tfa.GenerateSetupCode(Constants.ApplicationName, user.Email, accountSecretKey, 300, 300);

            var database          = DatabaseContext.Database;
            var twoFactorAuthInfo = new TwoFactorAuthInfo();
            var existingAccount   = database.Fetch <TwoFactor>(string.Format("WHERE userId = {0} AND [key] = '{1}'",
                                                                             user.Id, Constants.GoogleAuthenticatorProviderName));

            if (existingAccount.Any())
            {
                var account = existingAccount.First();
                if (account.Confirmed)
                {
                    return(twoFactorAuthInfo);
                }

                var tf = new TwoFactor {
                    Value = accountSecretKey, UserId = user.Id, Key = Constants.GoogleAuthenticatorProviderName
                };
                var update = database.Update(tf);

                if (update == 0)
                {
                    return(twoFactorAuthInfo);
                }
            }
            else
            {
                var result = database.Insert(new TwoFactor {
                    UserId = user.Id, Key = Constants.GoogleAuthenticatorProviderName, Value = accountSecretKey, Confirmed = false
                });
                if (result is bool == false)
                {
                    return(twoFactorAuthInfo);
                }

                var insertSucces = (bool)result;
                if (insertSucces == false)
                {
                    return(twoFactorAuthInfo);
                }
            }

            twoFactorAuthInfo.Secret          = setupInfo.ManualEntryKey;
            twoFactorAuthInfo.Email           = user.Email;
            twoFactorAuthInfo.ApplicationName = Constants.ApplicationName;

            return(twoFactorAuthInfo);
        }
    public List <TwoFactorAuthInfo> TwoFactorEnabled()
    {
        var database          = DatabaseContext.Database;
        var user              = Security.CurrentUser;
        var result            = database.Fetch <TwoFactor>("WHERE [userId] = @userId AND [confirmed] = 1", new { userId = user.Id });
        var twoFactorAuthInfo = new List <TwoFactorAuthInfo>();

        foreach (var factor in result)
        {
            var authInfo = new TwoFactorAuthInfo {
                ApplicationName = factor.Key
            };
            twoFactorAuthInfo.Add(authInfo);
        }
        return(twoFactorAuthInfo);
    }
    public List <TwoFactorAuthInfo> GetTwoFactorEnabled(int id)
    {
        using (var scope = scopeProvider.CreateScope(autoComplete: true))
        {
            var result = scope.Database.Fetch <TwoFactor>("WHERE [userId] = @userId AND [confirmed] = 1", new { userId = id });

            var twoFactorAuthInfo = new List <TwoFactorAuthInfo>();
            foreach (var factor in result)
            {
                var authInfo = new TwoFactorAuthInfo {
                    ApplicationName = factor.Key
                };
                twoFactorAuthInfo.Add(authInfo);
            }

            return(twoFactorAuthInfo);
        }
    }
    public TwoFactorAuthInfo GetExistingAccount(int userId, string googleAuthenticatorProviderName, string accountSecretKey)
    {
        var twoFactorAuthInfo = new TwoFactorAuthInfo();

        using (var scope = scopeProvider.CreateScope(autoComplete: true))
        {
            try
            {
                var existingAccount = scope.Database.Fetch <TwoFactor>(string.Format("WHERE userId = {0} AND [key] = '{1}'",
                                                                                     userId, Constants.GoogleAuthenticatorProviderName));

                if (existingAccount.Any())
                {
                    var account = existingAccount.First();
                    if (account.Confirmed)
                    {
                        return(twoFactorAuthInfo);
                    }

                    var tf = new TwoFactor {
                        Value = accountSecretKey, UserId = userId, Key = Constants.GoogleAuthenticatorProviderName
                    };
                    var update = scope.Database.Update(tf);

                    if (update == 0)
                    {
                        return(twoFactorAuthInfo);
                    }
                }
                else
                {
                    var result = scope.Database.Insert(new TwoFactor {
                        UserId = userId, Key = Constants.GoogleAuthenticatorProviderName, Value = accountSecretKey, Confirmed = false
                    });
                    if (result is bool == false)
                    {
                        return(twoFactorAuthInfo);
                    }

                    var insertSucces = (bool)result;
                    if (insertSucces == false)
                    {
                        return(twoFactorAuthInfo);
                    }
                }
            }
            catch (Exception e)
            {
                var result = scope.Database.Insert(new TwoFactor {
                    UserId = userId, Key = Constants.GoogleAuthenticatorProviderName, Value = accountSecretKey, Confirmed = false
                });
                if (result is bool == false)
                {
                    return(twoFactorAuthInfo);
                }

                var insertSucces = (bool)result;
                if (insertSucces == false)
                {
                    return(twoFactorAuthInfo);
                }
                //throw;
            }

            return(twoFactorAuthInfo);
        }
    }