示例#1
0
        public void ChangePassword(ChangePasswordParameter param)
        {
            using (var context = base.CreateUserContext())
            {
                EmailAuth  emailAuth  = null;
                MobileAuth mobileAuth = null;
                if (param.AuthCode != null)
                {
                    Guid emailAuthCode;
                    if (Guid.TryParse(param.AuthCode, out emailAuthCode))
                    {
                        emailAuth = this.CheckUserEmailAuth(context, emailAuthCode);
                    }
                    else
                    {
                        string[] mobileAuthCode = param.AuthCode.Split(',');
                        if (mobileAuthCode.Length != 2)
                        {
                            throw new InvalidInvokeException("参数错误");
                        }
                        mobileAuth     = this.CheckUserMobileAuth(context, mobileAuthCode[0], int.Parse(mobileAuthCode[1]));
                        param.UserName = mobileAuth.UserName;
                    }
                }

                var id = this.SignIn(new SignInParameter()
                {
                    AppID    = param.AppID,
                    UserName = param.UserName,
                    Password = param.OldPassword
                });
                if (!id.IsAuthenticated)
                {
                    throw new InvalidInvokeException("账户不存在或密码错误");
                }

                using (var scope = DbScope.Create())
                {
                    scope.BeginTransaction();

                    param.NewPassword = CryptoManaged.MD5Hex(param.NewPassword);
                    context.Accounts.Update(t => t.RowID == id.UserID, t => new Account()
                    {
                        Password = param.NewPassword
                    });
                    if (emailAuth != null)
                    {
                        emailAuth.Status = (int)ActivationStatus.Activated;
                    }
                    if (mobileAuth != null)
                    {
                        mobileAuth.Status = (int)ActivationStatus.Activated;
                    }
                    context.SaveChanges();

                    scope.Complete();
                }
            }
        }
示例#2
0
        /// <summary>
        /// 将手机验证吗写入数据库
        /// </summary>
        /// <param name="param"></param>
        public void SendAuthMobile(SendAuthMobileParameter param)
        {
            using (var context = base.CreateUserContext())
            {
                if (context.MobileAuths.Where(t => t.Mobile == param.Mobile && t.CreateDate.Date == DateTime.Now.Date).Count() > 2)
                {
                    throw new InvalidInvokeException("1天内已经发送超过3次,不再发送");
                }
                var q = from t in context.MobileAuths
                        where t.UserName == param.UserName && t.Mobile == param.Mobile
                        orderby t.CreateDate descending
                        select t.CreateDate;
                DateTime first = q.FirstOrDefault();
                if (first != DateTime.MinValue)
                {
                    TimeSpan ts = DateTime.Now - first;
                    if (ts.TotalSeconds <= 60)
                    {
                        throw new InvalidInvokeException("1分钟内仅发送1次");
                    }
                }

                var entity = new MobileAuth();
                EntityMapper.Map <SendAuthMobileParameter, MobileAuth>(param, entity);
                Random rnd = new Random();
                entity.SmsCode    = rnd.Next(1000, 9999).ToString();
                entity.CreateDate = DateTime.Now;
                entity.Status     = (int)ActivationStatus.NotActive;
                context.MobileAuths.Add(entity);
                context.SaveChanges();

                switch (param.Kind)
                {
                case AuthMobileKind.SignUp:
                    Utility.SendSignUpSMS(param.AppID, entity.Mobile, int.Parse(entity.SmsCode));
                    break;

                case AuthMobileKind.FindPassword:
                    Utility.SendFindPwdSMS(param.AppID, entity.Mobile, int.Parse(entity.SmsCode));
                    break;
                }
            }
        }