private void SeedData(MongoDatabase db)
        {
            if (db == null) throw new ArgumentNullException("db");

            // Reset db
            db.GetCollection("IDSequence").Drop();

            var rolesCol = db.GetCollection<Role>(Role.GetCollectionName());
            rolesCol.Drop();        // clear 1st

            var roles = new List<Role>
                            {
                                new Role("Admin"),
                                new Role("User"),
                                new Role("Guest")
                            };

            rolesCol.InsertBatch(roles);

            var usersCol = db.GetCollection<MembershipAccount>(MembershipAccount.GetCollectionName());
            usersCol.Drop();

            var user1 = new MembershipAccount("User1");
            user1.Roles.Add("Admin");  // this user is an Admin

            var users = new List<MembershipAccount>
                            {
                                user1,
                                new MembershipAccount("User2"),
                                new MembershipAccount("User3")
                            };

            usersCol.InsertBatch(users);

            Roles = rolesCol.AsQueryable();
            Users = usersCol.AsQueryable();
        }
 public void SaveUser(MembershipAccount user)
 {
     _usersCol.Save(user);
 }
        private void SeedData(MongoDatabase db)
        {
            if (db == null) throw new ArgumentNullException("db");

            // Reset db
            db.GetCollection("IDSequence").Drop();

            _usersCol = db.GetCollection<MembershipAccount>(MembershipAccount.GetCollectionName());
            _usersCol.Drop();

            var salt = Crypto.GenerateSalt();

            var user1 = new MembershipAccount("User1")
                            {
                                PasswordSalt =  salt,
                                Password = Crypto.HashPassword("p@ssword" + salt),
                                IsConfirmed = false
                            };

            var user2 = new MembershipAccount("NonLocalUser")
                            {
                                IsLocalAccount = false,
                                IsConfirmed = true
                            };

            _usersCol.InsertBatch(new[] { user1, user2 });

            var oAuthTokenCol = db.GetCollection<OAuthToken>(OAuthToken.GetCollectionName());
            oAuthTokenCol.Drop();
            oAuthTokenCol.Insert(new OAuthToken("Tok3n", "tok3nSecret"));

            var oAuthMembershipCol = db.GetCollection<OAuthMembership>(OAuthMembership.GetCollectionName());
            oAuthMembershipCol.Drop();
            oAuthMembershipCol.Insert( new OAuthMembership("Test", "User1@Test", 1) );

            Users = _usersCol.AsQueryable();
            OAuthTokens = oAuthTokenCol.AsQueryable();
            OAuthMemberships = oAuthMembershipCol.AsQueryable();
        }
        private bool SetPassword(MembershipAccount user, string newPassword, bool clearVerificationToken = false)
        {
            if (user == null || string.IsNullOrWhiteSpace(newPassword))
                return false;

            var salt = Crypto.GenerateSalt();

            user.PasswordSalt = salt;
            user.Password = Crypto.HashPassword(newPassword + salt);
            user.PasswordChangedDate = DateTime.UtcNow;

            if (clearVerificationToken)
            {
                user.PasswordVerificationToken = null;
                user.PasswordVerificationTokenExpirationDate = null;
            }

            return _context.Save(user);
        }
        private MembershipUser GetUser(MembershipAccount user)
        {
            if (user == null)
                return null;

            var lastLogin = user.LastLoginDate.HasValue ? user.LastLoginDate.Value : DateTime.MinValue;
            var lastPasswordChange = user.PasswordChangedDate.HasValue ? user.PasswordChangedDate.Value : DateTime.MinValue;

            // NOTE: This requires a valid system.web/membership section in config with matching provider name
            return new MembershipUser(_providerName, user.UserName, user.UserId, null, null, null, user.IsConfirmed /* isApproved */, false, user.CreateDate, lastLogin, DateTime.MinValue, lastPasswordChange, DateTime.MinValue);
        }
        private bool CheckPassword(MembershipAccount user, string password)
        {
            if (user == null || string.IsNullOrWhiteSpace(password))
                return false;

            var verificationSucceeded = Crypto.VerifyHashedPassword(user.Password, password + user.PasswordSalt);

            if (verificationSucceeded)
            {
                // Reset password failure count if applicable
                if (user.PasswordFailuresSinceLastSuccess > 0)
                {
                    user.PasswordFailuresSinceLastSuccess = 0;
                    _context.Save(user);
                }
            }
            else
            {
                user.PasswordFailuresSinceLastSuccess = user.PasswordFailuresSinceLastSuccess + 1;
                user.LastPasswordFailureDate = DateTime.UtcNow;
                _context.Save(user);
            }

            return verificationSucceeded;
        }
        public override string CreateUserAndAccount(string userName, string password, bool requireConfirmation, IDictionary<string, object> values)
        {
            VerifyInitialized();

            if (string.IsNullOrWhiteSpace(userName))
                throw new MembershipCreateUserException(MembershipCreateStatus.InvalidUserName);

            if (string.IsNullOrWhiteSpace(password))
                throw new MembershipCreateUserException(MembershipCreateStatus.InvalidPassword);

            var user = _context.GetUser(userName);

            // existing local accounts are duplicates
            if (user != null && user.IsLocalAccount)
                throw new MembershipCreateUserException(MembershipCreateStatus.DuplicateUserName);

            var salt = Crypto.GenerateSalt();
            var hashedPassword = Crypto.HashPassword(password + salt);

            if (hashedPassword.Length > 128)
                throw new MembershipCreateUserException(MembershipCreateStatus.InvalidPassword);

            // create a new local account
            if (user == null)
            {
                user = new MembershipAccount(userName)
                           {
                               PasswordSalt = salt,
                               Password = hashedPassword,
                               IsConfirmed = !requireConfirmation
                           };
            }
            else
            {
                // convert a non-local account
                user.IsLocalAccount = true;
                user.PasswordSalt = salt;
                user.Password = hashedPassword;
                user.IsConfirmed = !requireConfirmation;    // should already be confirmed
                user.PasswordChangedDate = DateTime.UtcNow;
                user.LastPasswordFailureDate = null;
                user.PasswordFailuresSinceLastSuccess = 0;
            }

            if (values != null)
                user.ExtraData = values.ToJson();

            try
            {
                _context.Save(user);
            }
            catch (Exception ex)
            {
                Trace.TraceError("MongoSimpleMembershipProvider.CreateUserAndAccount() ERROR: {0}", ex.ToString());
                throw new MembershipCreateUserException(MembershipCreateStatus.ProviderError);
            }

            return requireConfirmation ? user.ConfirmationToken : null;
        }
        public override void CreateOrUpdateOAuthAccount(string provider, string providerUserId, string userName)
        {
            VerifyInitialized();

            if (string.IsNullOrWhiteSpace(userName))
                throw new MembershipCreateUserException(MembershipCreateStatus.InvalidUserName);

            if (string.IsNullOrWhiteSpace(providerUserId))
                throw new MembershipCreateUserException(MembershipCreateStatus.InvalidProviderUserKey);     // not really the right status ??

            var user = _context.GetUser(userName);

            if (user == null)
            {
                // create a non-local account
                user = new MembershipAccount(userName)
                           {
                               IsConfirmed = true,
                               IsLocalAccount = false
                           };

                try
                {
                    _context.Save(user);
                }
                catch (Exception ex)
                {
                    Trace.TraceError("MongoSimpleMembershipProvider.CreateOrUpdateOAuthAccount() ERROR : {0}", ex.ToString());
                    throw new MembershipCreateUserException(MembershipCreateStatus.ProviderError);
                }
            }

            var oam = _context.GetOAuthMembership(provider, providerUserId);

            if (oam == null)
            {
                // account doesn't exist, create a new one.
                oam = new OAuthMembership(provider, providerUserId, user.UserId);
            }
            else
            {
                // account already exist, update it
                oam.UserId = user.UserId;
            }

            try
            {
                _context.Save(oam);
            }
            catch (Exception ex)
            {
                Trace.TraceError("MongoSimpleMembershipProvider.CreateOrUpdateOAuthAccount() ERROR : {0}", ex.ToString());
                throw new MembershipCreateUserException(MembershipCreateStatus.ProviderError);
            }
        }