public async Task <IResultModel <UserTokenResult> > LoginByName(AccountLoginModel model)
        {
            var result = new ResultModel <UserTokenResult>();

            if (model.LoginName.IsNull())
            {
                return(result.Failed("用户名不能为空"));
            }
            if (model.LoginPwd.IsNull())
            {
                return(result.Failed("登陆密码不能为空"));
            }
            var user = await _accountRepository.GetByLoginName(model.LoginName);

            if (!user.Status)
            {
                return(result.Failed("账户未启用"));
            }
            if (user.LoginPwd != Zoomtel.Utils.Helpers.Encrypt.DESEncrypt(model.LoginPwd, user.PassSalt))
            {
                return(result.Failed("账户密码不正确"));
            }
            //修改用户登陆信息
            user.LoginCount++;
            user.LastIp        = _loginInfo.IP;
            user.LastUserAgent = _loginInfo.UserAgent;
            await _accountRepository.UpdateAsync(user);// 这里要加上await 可能出现错误



            if (user != null)
            {
                var obj = Token(user);
                //记录登录日志
                //_loginLogRepository.Insert(new LoginLogEntity
                //{
                //    Id=_loginLogRepository.GetSeq("S_SYS_LOGINLOG").ToString(),
                //    Uid = user.Uid,
                //    LoginIp = user.LastIp,
                //    LoginTime = DateTime.Now,
                //    AccountName = user.LoginName,
                //    Token=obj.access_token


                //});
                _loginLogService.Add(new LoginLogEntity
                {
                    Uid       = user.Uid,
                    LoginIp   = user.LastIp,
                    LoginTime = DateTime.Now,
                    LoginName = user.LoginName,
                    Token     = obj.access_token
                });
                return(result.Success(obj));
            }
            else
            {
                return(result.Failed("用户名不存在"));
            }
        }
Beispiel #2
0
        public IHttpActionResult Login([FromBody] LoginLog getmodel)
        {
            /**所需参数  2018-09-25
             *  LoginTel:登录手机号
             *   SmsID:短信ID
             *   VerCode:短信验证码
             *   头部所需auth:token
             * */
            LoginLog     logModel = new LoginLog();
            GetSmsReturn retmodel = new GetSmsReturn();

            //手机号或者验证码或短信ID为空 返回400错误 缺少参数
            if (string.IsNullOrEmpty(getmodel.LoginTel) || string.IsNullOrEmpty(getmodel.SmsID))
            {
                AuthResponse retResponse = new AuthResponse();
                retResponse.Code      = "400";
                retResponse.Message   = "参数错误";
                retResponse.NowTime   = DateTime.Now;
                retResponse.data      = "";
                retResponse.IsSuccess = false;
                return(Ok(retResponse));
            }

            //获取解析过的token值 包含手机号和短信ID
            AuthInfo info = RequestContext.RouteData.Values["auth"] as AuthInfo;

            //根据smsid和手机号 查询验证码
            retmodel = gsSer.Where(a => a.smsId == getmodel.SmsID && a.getSmsTel == getmodel.LoginTel).FirstOrDefault();
            //根据传过来的手机号和验证码判断是否匹配并进行登录
            if (info.UserTel == getmodel.LoginTel && retmodel.VerCode == getmodel.VerCode)
            {
                logModel.LoginDate   = DateTime.Now;
                logModel.VerCode     = getmodel.VerCode;
                logModel.ErrorCode   = "0000";
                logModel.ErrorDec    = "登录成功";
                logModel.LoginRemark = "登录成功";
                logModel.LoginTel    = getmodel.LoginTel;
                logModel.SmsID       = getmodel.SmsID;
                logSer.Add(logModel);
                //返回接口json
                AuthResponse retResponse = new AuthResponse();
                retResponse.Code      = "200";
                retResponse.Message   = "调用接口成功";
                retResponse.NowTime   = DateTime.Now;
                retResponse.IsSuccess = true;
                retResponse.data      = JsonConvert.SerializeObject(logModel);
                return(Ok(retResponse));
            }
            else
            {
                logModel.LoginDate   = DateTime.Now;
                logModel.VerCode     = getmodel.VerCode;
                logModel.ErrorCode   = "0001";
                logModel.ErrorDec    = "登录失败";
                logModel.LoginRemark = "验证码和手机号不匹配";
                logModel.LoginTel    = getmodel.LoginTel;
                logModel.SmsID       = getmodel.SmsID;
                logSer.Add(logModel);
                //统一格式 返回接口json
                AuthResponse retResponse = new AuthResponse();
                retResponse.Code      = "200";
                retResponse.Message   = "调用接口成功";
                retResponse.NowTime   = DateTime.Now;
                retResponse.IsSuccess = true;
                retResponse.data      = JsonConvert.SerializeObject(logModel);
                return(Ok(retResponse));
            }
        }
        private void AjaxLogin(string xname, string xpassword, HttpContext context)
        {
            try
            {
                var server = new MemberService();
                string password = DESEncrypt.Encrypt(xpassword, xname);
                var member = server.FirstOrDefault(m => m.UserName == xname && m.Password == password && m.IsDelete == false);
                if (member != null)
                {
                    if (member.IsUsing == false)
                    {
                        _response = "{\"result\" :\"" + 0 + "\",\"returnval\" :\"" + "用户已锁定,请联系管理员!" + "\"}";
                        return;
                    }
                    member.sys_MemberExtend.IsOnline = true;
                    member.sys_MemberExtend.LastLoginIP = GetIP;
                    member.sys_MemberExtend.LastLoginTime = DateTime.Now;

                    var ipserver = new ForbidIpService();
                    var ipList = ipserver.Where(i => i.IsUsing && i.IsDelete == false).ToList().Select(q => q.IP);
                    if (member.UserName != "admin" && ipList.Contains(member.sys_MemberExtend.LastLoginIP))
                    {
                        _response = "{\"result\" :\"" + 0 + "\",\"returnval\" :\"" + "您的IP地址已锁定,请联系管理员!" + "\"}";
                        return;
                    }

                    CreateFormsAuthenticationTicket(context, member, false, DateTime.Now.AddMinutes(120));
                    server.SaveChanges();
                    var loginlogservice = new LoginLogService();
                    var loginlog = new sys_LoginLog
                    {
                        Uid = member.id,
                        RealName = member.RealName,
                        Ip = GetIP,
                        Address = "",
                        Mac = "",
                        LoginTime = DateTime.Now
                    };
                    loginlogservice.Add(loginlog);
                    _response = "{\"result\" :\"" + 1 + "\",\"returnval\" :\"" + "登录成功,正在转到主页..." + "\"}";
                }
                else
                    _response = "{\"result\" :\"" + 0 + "\",\"returnval\" :\"" + "用户名或者密码不正确!" + "\"}";

            }
            catch (Exception ex)
            {
                _response = "{\"result\" :\"" + 0 + "\",\"returnval\" :\"" + ex.Message + "\"}";
            }

        }