Exemplo n.º 1
0
        public async Task <string> UploadFile()
        {
            try
            {
                var file = Request.Form.Files[0];


                var userCheck = (User)RouteData.Values[ParseDataKeyApi.KEY_PASS_DATA_USER_MODEL];

                if (file.Length > 2097152)
                {
                    return(CreateDataError("File max size 2Mb"));
                }

                if (file.Length <= 0)
                {
                    return(CreateDataError("Can't update image"));
                }


                using (var w = new WebClient())
                {
                    w.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
                    try
                    {
                        using (var m = new MemoryStream())
                        {
                            file.CopyTo(m);
                            m.Close();
                            // Convert byte[] to Base64 String
                            var base64String = Convert.ToBase64String(m.GetBuffer());

                            var values = new NameValueCollection
                            {
                                { ParseDataKeyApi.KEY_USER_UPDATE_AVATAR, base64String }
                            };

                            w.Headers.Add("Authorization", "Client-ID " + AppSettingHelper.GetImgurApiKey());

                            byte[] response = await w.UploadValuesTaskAsync(AppSettingHelper.GetImgurUrl(), values);

                            var result = JsonHelper.DeserializeObject <JObject>(Encoding.UTF8.GetString(response));

                            if (!(bool)result["success"])
                            {
                                return(CreateDataError("Save image fail"));
                            }
                            userCheck.Avatar = result["data"]["link"].ToString();
                            var updateUser = _userBusiness.UpdateProfile(userCheck);

                            if (updateUser.Status != Status.STATUS_SUCCESS)
                            {
                                return(CreateDataError("Can't update image"));
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                        throw;
                    }

                    //save action log
                    _userBusiness.AddActionLog(userCheck.Email, userCheck.Id,
                                               ActionLog.AVATAR,
                                               HelpersApi.GetIp(Request));

                    return(new ReturnObject
                    {
                        Status = Status.STATUS_SUCCESS,
                        Message = "Upload avatar success ",
                        Data = userCheck.Avatar
                    }.ToJson());
                }
            }
            catch (Exception e)
            {
                _logger.Error(KeyLogger.USER_AVATAR + e);
                return(CreateDataError(e.Message));
            }
        }
Exemplo n.º 2
0
        public string UpdateOption([FromBody] JObject value)
        {
            try
            {
                var userModel = (User)RouteData.Values[ParseDataKeyApi.KEY_PASS_DATA_USER_MODEL];

                if (!value.ContainsKey(ParseDataKeyApi.KEY_TWO_FA_UPDATE_OPTION))
                {
                    return(HelpersApi.CreateDataError(MessageApiError.PARAM_INVALID));
                }

                var code = "";
                if (value.ContainsKey(ParseDataKeyApi.KEY_TWO_FA_UPDATE_OPTION_CODE))
                {
                    code = value[ParseDataKeyApi.KEY_TWO_FA_UPDATE_OPTION_CODE].ToString();
                }


                bool isVerify = false;

                switch (userModel.IsTwoFactor)
                {
                case 1:
                    if (!value.ContainsKey(ParseDataKeyApi.KEY_PASS_DATA_GET_CODE))
                    {
                        return(HelpersApi.CreateDataError(MessageApiError.PARAM_INVALID));
                    }

                    isVerify = HelpersApi.CheckCodeGoogle(userModel.TwoFactorSecret, code);
                    break;

                case 2:
                    if (!value.ContainsKey(ParseDataKeyApi.KEY_PASS_DATA_GET_CODE))
                    {
                        return(HelpersApi.CreateDataError(MessageApiError.PARAM_INVALID));
                    }

                    var secretAuthToken = ActionCode.FromJson(userModel.SecretAuthToken);
                    if (string.IsNullOrEmpty(secretAuthToken.UpdateOptionVerification))
                    {
                        return(HelpersApi.CreateDataError(MessageApiError.SMS_VERIFY_ERROR));
                    }

                    isVerify = HelpersApi.CheckCodeSms(secretAuthToken.UpdateOptionVerification, code, userModel);
                    break;

                case 0:
                    isVerify = true;
                    break;
                }


                if (!isVerify)
                {
                    return(HelpersApi.CreateDataError(MessageApiError.SMS_VERIFY_ERROR));
                }

                var option = value[ParseDataKeyApi.KEY_TWO_FA_UPDATE_OPTION];

                userModel.Verification = (int)option;

                _userBusiness.AddActionLog(userModel.Email, userModel.Id,
                                           ActionLog.UPDATE_OPTION_VETIFY,
                                           HelpersApi.GetIp(Request));

                return(_userBusiness.UpdateProfile(userModel).ToJson());
            }
            catch (Exception e)
            {
                _logger.Error(KeyLogger.TWOFA_OPTION_UPDATE + e);
                return(HelpersApi.CreateDataError(e.Message));
            }
        }