Exemplo n.º 1
0
        public override bool ChangePassword(string username, string oldPassword, string newPassword)
        {
            oldPassword = oldPassword.Trim();
            if (!SecurityUtility.IsPasswordValid(newPassword))
            {
                return(false);
            }
            var user = SecurityUtility.GetUserByUsername(username);

            if (user == null)
            {
                return(false);
            }
            var feedbackMessage = new System.Text.StringBuilder();

            if (user.Password != oldPassword && user.Password != Cryptography.EncryptPassword(oldPassword, user.Salt))
            {
                return(false);
            }

            user.Password = Cryptography.EncryptPassword(newPassword, user.Salt);
            IRepositoryProvider _repositoryProvider = new RepositoryProvider(new RepositoryFactories());
            var unitofWork = new UnitOfWork(new MisukaDBContext(), _repositoryProvider);

            unitofWork.Repository <Domain.Entity.User>().Update(user);
            var ret = unitofWork.SaveChanges();

            return(ret > 0);
        }
Exemplo n.º 2
0
        //public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
        //{
        //  throw new NotImplementedException();
        //}

        public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
        {
            #region Check valid username
            if (!SecurityUtility.IsUsernameValid(username))
            {
                status = MembershipCreateStatus.InvalidUserName;
                return(null);
            }
            #endregion

            #region Check for valid PersonId

            string _username = username;
            string _domain   = string.Empty;

            try
            {
                if (SecurityUtility.UsernameContainsDomain(username))
                {
                    _username = SecurityUtility.ExtractUsername(username);
                    _domain   = SecurityUtility.ExtractDomain(username);
                }
            }
            catch
            {
                // Username was flagged as having a domain attached, but the extraction failed for unknown reason
                status = MembershipCreateStatus.InvalidUserName;
                return(null);
            }

            Guid personId;
            bool locked = false;
            try
            {
                if (providerUserKey != null && providerUserKey.ToString().Length >= 32)
                {
                    personId = new Guid(providerUserKey.ToString().Substring(0, 32));
                    locked   = ValueUtilities.GetBoolean(providerUserKey.ToString().Substring(32), false);
                }
                else if (providerUserKey == null || providerUserKey.ToString().Length == 0)
                {
                    personId = Guid.Empty;
                }
                else
                {
                    status = MembershipCreateStatus.InvalidProviderUserKey;
                    return(null);
                }
            }
            catch
            {
                status = MembershipCreateStatus.InvalidProviderUserKey;
                return(null);
            }

            #endregion

            #region Test for valid email
            //if ((RequiresUniqueEmail || EnablePasswordRetrieval || EnablePasswordReset) && !password.Equals(SOCIAL_LOGIN_DEFAULT_PASSWORD))
            //{

            //  if (email == null || EmailUtilities.ValidateEmailAddress(email) == false)
            //  {
            //    status = MembershipCreateStatus.InvalidEmail;
            //    return null;
            //  }
            //}
            #endregion

            #region Test for valid password


            if (!SecurityUtility.IsPasswordValid(password))
            {
                status = MembershipCreateStatus.InvalidPassword;
                return(null);
            }


            #endregion

            IRepositoryProvider _repositoryProvider = new RepositoryProvider(new RepositoryFactories());
            var unitofWork = new UnitOfWork(new MisukaDBContext(), _repositoryProvider);

            #region Check for unique username
            Domain.Entity.User user = unitofWork.Repository <User>().Query(u => String.Compare(u.UserName, username, StringComparison.InvariantCultureIgnoreCase) == 0).Select().FirstOrDefault();
            if (user != null)
            {
                status = MembershipCreateStatus.DuplicateUserName;
                return(null);
            }
            #endregion

            #region Test for valid question/answer
            if (RequiresQuestionAndAnswer)
            {
                if (passwordQuestion == null || passwordQuestion.Length > 200 || passwordQuestion.Length < 1)
                {
                    status = MembershipCreateStatus.InvalidQuestion;
                    return(null);
                }

                if (passwordAnswer == null || passwordAnswer.Length > 200 || passwordAnswer.Length < 1)
                {
                    status = MembershipCreateStatus.InvalidAnswer;
                    return(null);
                }
            }

            #endregion

            DateTime dt = DateTime.Now;
            user = new User
            {
                UserName         = _username,
                CreationDate     = dt,
                Domain           = _domain,
                PersonId         = personId,
                Locked           = locked,
                FailedLoginTimes = 0,
                CurrentLanguage  = System.Threading.Thread.CurrentThread.CurrentCulture.ToString()
            };


            user.Password = Cryptography.EncryptPassword(password, user.Salt);
            try
            {
                unitofWork.RepositoryAsync <Domain.Entity.User>().Insert(user);
                unitofWork.SaveChanges();
            }
            catch
            {
                status = MembershipCreateStatus.UserRejected;
                //  Log.Debug(this, string.Format("Create new user: {0} - failed", identity.Username));
                return(null);
            }

            status = MembershipCreateStatus.Success;
            //Log.Debug(this, string.Format("Create new user: {0} - successfully", identity.Username));
            return(new MembershipUser(_providerName, username, providerUserKey, email, passwordQuestion, "", isApproved, false, dt, dt, dt, dt, DateTime.MinValue));
        }