示例#1
0
        public ApplicationUser RegisterUser(string email, string password, string mobile, string lastName, string firstName)
        {
            email = email.ToLower();

            if (!IsEmailUnique(email))
            {
                throw new PropertyNotUniqueException("Email",email);
            }

            try
            {
                
                DatabaseService.BeginTransaction();
                var salt = StringHelper.GenerateSalt();
                var hash = StringHelper.GetHash(password, salt, MD5.Create());

                var user = new ApplicationUser
                {
                    UserName = email,
                    //LastName = lastName.ToTitleCase(),
                    //FirstName = firstName.ToTitleCase(),
                    //Mobile = mobile,
                    //Salt = salt,
                    //Hash = hash,
                    Active = true,
                };

                var challenge = new UserChallenge(StringHelper.GenerateSalt(64));
                user.UserChallenge = challenge;

                var wallet = new Wallet();
                DatabaseService.Save(user);
                wallet.User = user;
                DatabaseService.Save(wallet);

                DatabaseService.CommitTransaction();
                return user;
            }
            catch (System.Exception)
            {
                DatabaseService.RollBackTransaction();
                throw;
            }
        }
示例#2
0
        public UserChallengeStatus ChallengeUser(string id, string challenge)
        {
            UserChallengeStatus status;

            var user = DatabaseService.Get<ApplicationUser>(id);
            if (user != null)
            {
                if (!user.IsVerified)
                {
                    if (user.UserChallenge != null)
                    {
                        if (user.UserChallenge.CreatedAt.AddDays(1) <= DateTime.UtcNow)
                        {
                            status = UserChallengeStatus.Expired;
                        }
                        else if (user.UserChallenge.TryCount >= 5)
                        {
                            status = UserChallengeStatus.OverLimit;
                        }
                        else
                        {
                            try
                            {
                                DatabaseService.BeginTransaction();
                                if (user.UserChallenge.Challenge.Equals(challenge, StringComparison.OrdinalIgnoreCase))
                                {
                                    user.IsVerified = true;
                                    user.UserChallenge.VerifiedAt = DateTime.UtcNow;
                                    status = UserChallengeStatus.Successed;
                                    DatabaseService.Save(user);
                                    DatabaseService.Save(user.UserChallenge);
                                }
                                else
                                {
                                    user.UserChallenge.TryCount++;
                                    status = UserChallengeStatus.Mismatch;
                                    DatabaseService.Save(user.UserChallenge);
                                }
                                DatabaseService.CommitTransaction();
                            }
                            catch (System.Exception)
                            {
                                DatabaseService.RollBackTransaction();
                                status = UserChallengeStatus.UnknownException;
                            }
                            
                        }
                    }
                    else
                    {
                        var userChallenge = new UserChallenge(StringHelper.GenerateSalt(64));
                        user.UserChallenge = userChallenge;
                        DatabaseService.Save(user);
                        status = UserChallengeStatus.TryAgain;
                    }
                }
                else
                {
                    status = UserChallengeStatus.AlreadyVerified;
                }
            }
            else
            {
                status = UserChallengeStatus.NotFound;
            }
            return status;
        }