public override bool DeleteUser(string username, bool deleteAllRelatedData)
 {
     try
     {
         UnpairAccount(username);
     }
     catch { }
     return(InnerMembershipProvider.DeleteUser(username, deleteAllRelatedData));
 }
        /// <summary>
        /// Returns the Latch account ID associated with a membership username
        /// </summary>
        /// <param name="username">The membership username to query the account ID</param>
        /// <returns>the Latch account ID</returns>
        public string GetUserAccountId(string username)
        {
            var user = InnerMembershipProvider.GetUser(username, false);

            if (user == null)
            {
                throw new ArgumentException("Invalid username");
            }
            return(GetUserAccountId(user));
        }
        /// <summary>
        /// Verifies that the specified user name and password are valid in the inner membership provider and then performs a Latch verification
        /// </summary>
        /// <param name="username">The name of the user to validate.</param>
        /// <param name="password">The password for the specified user</param>
        /// <returns>true if the specified username and password are valid and the account is not disabled in Latch; otherwise, false</returns>
        public override bool ValidateUser(string username, string password)
        {
            if (InnerMembershipProvider.ValidateUser(username, password))
            {
                var    user      = InnerMembershipProvider.GetUser(username, false);
                string accountId = GetUserAccountId(user);

                if (this.latchAPI != null && !string.IsNullOrEmpty(accountId))
                {
                    var statusResponse = this.latchAPI.OperationStatus(accountId, this.loginOperation);

                    if (statusResponse.Data != null && statusResponse.Data.ContainsKey("operations"))
                    {
                        var operations = (Dictionary <string, object>)statusResponse.Data["operations"];
                        if (operations.ContainsKey(this.loginOperation))
                        {
                            var app = (Dictionary <string, object>)operations[this.loginOperation];
                            if (app.ContainsKey("status"))
                            {
                                string status = app["status"].ToString().ToLower();

                                if (status == "on")
                                {
                                    if (app.ContainsKey("two_factor"))
                                    {
                                        var    twoFactor = (Dictionary <string, object>)app["two_factor"];
                                        string token     = ((Dictionary <string, object>)twoFactor)["token"].ToString();
                                        if (!string.IsNullOrEmpty(token))
                                        {
                                            OnSecondFactorRequired(new SecondFactorEventArgs(username, token));
                                        }
                                    }
                                }
                                else
                                {
                                    return(false);
                                }
                            }
                        }
                    }
                }
                return(true);
            }
            else
            {
                return(false);
            }
        }
        /// <summary>
        /// Unpairs (deassociates) the username account with Latch
        /// </summary>
        /// <param name="username">the membership username to unpair</param>
        public void UnpairAccount(string username)
        {
            var    user      = InnerMembershipProvider.GetUser(username, false);
            string accountId = GetUserAccountId(user);

            var pairingInfo = FindPairingInfoByAccountId(accountId);

            if (this.latchAPI != null && user != null && !string.IsNullOrEmpty(accountId) && pairingInfo != null)
            {
                if (pairingInfo.Length == 1)
                {
                    var unpairResponse = this.latchAPI.Unpair(accountId);
                    if (unpairResponse.Error != null)
                    {
                        throw new ApplicationException(unpairResponse.Error.ToString());
                    }
                }

                SetUserAccountId(user, string.Empty);
            }
        }
        /// <summary>
        /// Pairs (associates) the username account with Latch
        /// </summary>
        /// <param name="username">The membership username to pair</param>
        /// <param name="pairingToken">The pairing authentication token generated by Latch and received by the user in its mobile device</param>
        /// <returns>the account ID returned by the server</returns>
        public string PairAccount(string username, string pairingToken)
        {
            var user = InnerMembershipProvider.GetUser(username, false);

            if (user == null)
            {
                throw new ArgumentException("Invalid username");
            }

            var pairResponse = LatchAPI.Pair(pairingToken);

            if (pairResponse.Data != null && pairResponse.Data.ContainsKey("accountId"))
            {
                string accountId = pairResponse.Data["accountId"].ToString();
                SetUserAccountId(user, accountId);
                return(accountId);
            }
            else if (pairResponse.Error != null)
            {
                throw new ApplicationException(pairResponse.Error.ToString());
            }
            return(null);
        }
 public override void UpdateUser(MembershipUser user)
 {
     InnerMembershipProvider.UpdateUser(user);
 }
 public override bool UnlockUser(string userName)
 {
     return(InnerMembershipProvider.UnlockUser(userName));
 }
 public override string ResetPassword(string username, string answer)
 {
     return(InnerMembershipProvider.ResetPassword(username, answer));
 }
 public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)
 {
     return(InnerMembershipProvider.ChangePasswordQuestionAndAnswer(username, password, newPasswordQuestion, newPasswordAnswer));
 }
 public override MembershipUser GetUser(string username, bool userIsOnline)
 {
     return(InnerMembershipProvider.GetUser(username, userIsOnline));
 }
 public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
 {
     return(InnerMembershipProvider.GetUser(providerUserKey, userIsOnline));
 }
 public override int GetNumberOfUsersOnline()
 {
     return(InnerMembershipProvider.GetNumberOfUsersOnline());
 }
 public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
 {
     return(InnerMembershipProvider.GetAllUsers(pageIndex, pageSize, out totalRecords));
 }
 public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
 {
     return(InnerMembershipProvider.FindUsersByEmail(usernameToMatch, pageIndex, pageSize, out totalRecords));
 }
 public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
 {
     return(InnerMembershipProvider.CreateUser(username, password, email, passwordQuestion, passwordAnswer, isApproved, providerUserKey, out status));
 }
 public override string GetUserNameByEmail(string email)
 {
     return(InnerMembershipProvider.GetUserNameByEmail(email));
 }
 public override bool ChangePassword(string username, string oldPassword, string newPassword)
 {
     return(InnerMembershipProvider.ChangePassword(username, oldPassword, newPassword));
 }