public static BaseActionResult CreateUserAccessToken(UserAccessToken obj4create)
        {
            string msg;
            if (obj4create == null)
            {
                msg = string.Format(XiaoluResources.MSG_CREATE_SUCCESS, XiaoluResources.STR_USER_ACCESS_TOKEN) + string.Format(XiaoluResources.STR_FAIL_RESAON, XiaoluResources.MSG_OBJECT_IS_NULL);
                return new BaseActionResult(false, msg);
            }

            try
            {
                using (var context = new XiaoluEntities())
                {
                    var repository = new UserAccessTokenRepository(context);
                    string newId = Guid.NewGuid().ToString();
                    obj4create.Id = newId;
                    repository.Create(obj4create);
                    context.SaveChanges();
                    msg = string.Format(XiaoluResources.MSG_CREATE_SUCCESS, obj4create.AccessToken);
                    return new BaseActionResult(true, msg);
                }
            }
            catch (Exception e)
            {
                msg = string.Format(XiaoluResources.MSG_CREATE_FAIL, obj4create.AccessToken) + string.Format(XiaoluResources.STR_FAIL_RESAON, ExceptionHelper.GetInnerExceptionInfo(e));
                return new BaseActionResult(false, msg);
            }
        }
        public static BaseActionResult DeleteUserAccessToken(UserAccessToken obj4delete)
        {
            using (var context = new XiaoluEntities())
            {
                string msg;
                var repository = new UserAccessTokenRepository(context);

                if (obj4delete == null)
                {
                    msg = string.Format(XiaoluResources.MSG_DELETE_SUCCESS, XiaoluResources.STR_USER_ACCESS_TOKEN) + string.Format(XiaoluResources.STR_FAIL_RESAON, XiaoluResources.MSG_OBJECT_IS_NULL);
                    return new BaseActionResult(false, msg);
                }
                repository.Delete(obj4delete);
                context.SaveChanges();
                msg = string.Format(XiaoluResources.MSG_UPDATE_SUCCESS, obj4delete.AccessToken);
                return new BaseActionResult(true, msg);
            }
        }
        public static bool _isMatch(UserAccessToken obj, UserAccessTokenQuery query)
        {
            if (!string.IsNullOrEmpty(query.IdEqual) && !string.Equals(obj.Id, query.IdEqual))
                return false;
            if (!string.IsNullOrEmpty(query.IdNotEqual) && string.Equals(obj.Id, query.IdNotEqual))
                return false;

            if (!string.IsNullOrEmpty(query.UserIdEqual) && !string.Equals(obj.UserId, query.UserIdEqual))
                return false;

            if (!string.IsNullOrEmpty(query.AccessIdEqual) && !string.Equals(obj.AccessToken, query.AccessIdEqual))
                return false;

            if (!string.IsNullOrEmpty(query.WeixinIdEqual) && !string.Equals(obj.WeixinId, query.WeixinIdEqual))
                return false;

            return true;
        }
 private static dynamic _orderByKey(UserAccessToken obj, UserAccessTokenQuery query)
 {
     if (string.IsNullOrEmpty(query.OrderByKey))
         return obj.Id;
     return obj.GetType().GetProperty(query.OrderByKey).GetValue(obj);
 }
Example #5
0
        // POST api/login
        public Object Post(LoginApiModel obj)
        {
            string msg;
            try
            {
                if (string.IsNullOrEmpty(obj.UserIdOrMobile))
                {
                    msg = XiaoluResources.MSG_LOGIN_FAIL + string.Format(XiaoluResources.STR_FAIL_RESAON, XiaoluResources.MSG_MOBILE_OR_USERID_IS_NULL);
                    return new { IsSuccess = false, Message = msg };
                }

                if (string.IsNullOrEmpty(obj.Password) || obj.Password.Trim().Length < 6)
                {
                    msg = XiaoluResources.MSG_LOGIN_FAIL + string.Format(XiaoluResources.STR_FAIL_RESAON, XiaoluResources.MSG_PASSWORD_IS_NOT_VALID);
                    return new { IsSuccess = false, Message = msg };
                }

                User userInDb = BusinessService.GetUserByName(obj.UserIdOrMobile);
                User userInDb2 = BusinessService.GetUserByMobile(obj.UserIdOrMobile);
                if (userInDb == null && userInDb2 == null )
                {
                    msg = XiaoluResources.MSG_LOGIN_FAIL + string.Format(XiaoluResources.STR_FAIL_RESAON, XiaoluResources.MSG_CANNOT_FIND_USER);
                    return new { IsSuccess = false, Message = msg };
                }

                User findedUser = (userInDb == null) ? userInDb2 : userInDb;
                string md5Pwd = FormsAuthentication.HashPasswordForStoringInConfigFile(obj.Password, "MD5");
                //验证密码
                if (!string.Equals(md5Pwd, findedUser.Password))
                {
                    findedUser.LastFailLoginTime = DateTime.Now;
                    if (findedUser.ErrLoginTimes == null)
                    {
                        findedUser.ErrLoginTimes = 0;
                    }
                    findedUser.ErrLoginTimes++;
                    BusinessService.UpdateUser(findedUser);

                    msg = XiaoluResources.MSG_LOGIN_FAIL + string.Format(XiaoluResources.STR_FAIL_RESAON, XiaoluResources.MSG_PASSWORD_IS_IN_CORRECT);
                    return new { IsSuccess = false, Message = msg };
                }
                //密码验证通过,则登录成功
                //写历史记录
                msg = XiaoluResources.MSG_LOGIN_SUCCESS;
                History his = new History()
                {
                    UserId = findedUser.Name,
                    CreationDate = DateTime.Now,
                    Content = msg
                };
                BusinessService.CreateHistory(his);

                string accessToken4User;
                UserAccessToken uatInDb = BusinessService.GetAccessTokenByUserId(findedUser.Name);
                if (uatInDb != null && uatInDb.ExpireDate > DateTime.Now)
                {
                    accessToken4User = uatInDb.AccessToken;
                    return new { IsSuccess = true, Message = msg, AccessToken = accessToken4User };
                }
                if (uatInDb != null)
                {
                    BusinessService.DeleteUserAccessToken(uatInDb);
                }

                accessToken4User = Guid.NewGuid().ToString();
                UserAccessToken uat = new UserAccessToken()
                {
                    UserId = findedUser.Name,
                    AccessToken = accessToken4User,
                    WeixinId = findedUser.WeixinId,
                    ExpireDate = DateTime.Now.AddSeconds(ACCESS_TOKEN_DURATION_IN_SECONDS)
                };
                BusinessService.CreateUserAccessToken(uat);

                AccessTokenUserPool.AddAccessTokenUserId(accessToken4User, findedUser.Name);
                return new { IsSuccess = true, Message = msg, AccessToken = accessToken4User };
            }
            catch (Exception e)
            {
                msg = XiaoluResources.MSG_LOGIN_FAIL + string.Format(XiaoluResources.STR_FAIL_RESAON, ExceptionHelper.GetInnerExceptionInfo(e));
                return new { IsSuccess = false, Message = msg };
            }
        }