Example #1
0
        /// <summary>
        /// 后端用户登录操作
        /// </summary>
        /// <param name="model">AccountViewModel实体</param>
        /// <returns></returns>
        public ResponsResult Login(BackstageUserAdd model)
        {
            ResponsResult result      = new ResponsResult();
            string        sessionCode = string.Empty;

            try
            {
                var code = CookieUtility.GetCookie(TbConstant.WEBSITE_VERIFICATION_CODE);
                if (code != null)
                {
                    sessionCode = ServiceCollectionExtension.Decrypt(code);
                }
            }
            catch (Exception ex)
            {
                Log4Net.Debug(ex);
            }
            if (model.ErrCount >= 3)
            {
                if (!model.VerCode.ToString().ToLower().Equals(sessionCode.ToLower()))
                {
                    return(result.SetStatus(ErrorCode.NotFound, "验证码输入不正确!"));
                }
            }

            BackstageUser account = this.First <BackstageUser>(t => t.LoginName == model.LoginName);

            if (account == null)
            {
                return(result.SetStatus(ErrorCode.NotFound, "账号不存在!"));
            }
            string pwd = Security.MD5(model.Password);

            if (!account.Password.Equals(pwd, StringComparison.OrdinalIgnoreCase))
            {
                return(result.SetStatus(ErrorCode.InvalidPassword));
            }
            switch (account.AccountStatus)
            {
            case (int)AccountStatus.Disabled:
                return(result.SetStatus(ErrorCode.AccountDisabled, "账号不可用!"));
            }

            account.LastLoginTime = DateTime.Now;
            account.LastLoginIp   = "";
            this.Update(account, true);
            MvcIdentity identity = new MvcIdentity(account.Id, account.LoginName, account.LoginName, account.Email, (int)account.AccountType, null, account.LastLoginTime);

            identity.Login(TbConstant.WEBSITE_AUTHENTICATION_SCHEME, x =>
            {
                x.Expires  = DateTime.Now.AddHours(25);//滑动过期时间
                x.HttpOnly = true;
            });

            return(result);
        }
        public MyResult <object> Login(BackstageUserAdd model)
        {
            MyResult result      = new MyResult();
            string   sessionCode = string.Empty;

            try
            {
                var code = CookieUtil.GetCookie(Constants.WEBSITE_VERIFICATION_CODE);
                if (code != null)
                {
                    sessionCode = DataProtectionUtil.UnProtect(code);
                }
            }
            catch (Exception ex)
            {
                LogUtil <AccountService> .Error(ex.Message);
            }
            if (model.ErrCount >= 3)
            {
                if (!model.VerCode.ToString().ToLower().Equals(sessionCode.ToLower()))
                {
                    return(result.SetStatus(ErrorCode.NotFound, "验证码输入不正确!"));
                }
            }

            BackstageUser account = this.First <BackstageUser>(t => t.LoginName == model.LoginName);

            if (account == null)
            {
                return(result.SetStatus(ErrorCode.NotFound, "账号不存在!"));
            }
            string pwd = SecurityUtil.MD5(model.Password);

            if (!account.Password.Equals(pwd, StringComparison.OrdinalIgnoreCase))
            {
                return(result.SetStatus(ErrorCode.InvalidPassword));
            }
            switch (account.AccountStatus)
            {
            case (int)AccountStatus.Disabled:
                return(result.SetStatus(ErrorCode.AccountDisabled, "账号不可用!"));
            }

            account.LastLoginTime = DateTime.Now;
            account.LastLoginIp   = "";//MvcHelper.ClientIP;
            this.Update(account, true);
            MvcIdentity identity = new MvcIdentity(account.Id, account.LoginName, account.LoginName, account.Email, (int)account.RoleId, null, account.LastLoginTime);

            identity.Login(Constants.WEBSITE_AUTHENTICATION_SCHEME, x =>
            {
                x.Expires  = DateTime.Now.AddHours(5);//滑动过期时间
                x.HttpOnly = true;
            });

            return(result);
        }