示例#1
0
        public async Task <IActionResult> UpdateIdNumber(string fullName, string idNumber)
        {
            try
            {
                _log.LogInformation("UPDATE IDNUMBER - START");
#if DEBUG
                var userId = _userManager.FindByNameAsync("admin").Result.Id;
#else
                var userId = User.GetSpecificClaim("UserId");
#endif
                //Update user Verify
                if (await _appUserService.UpdateIdNumberAndFullName(userId, fullName, idNumber))
                {
                    _log.LogInformation("UPDATE IDNUMBER SUCCESS - END");
                    return(new OkObjectResult(ApiResponseModel.GetSuccessModel("Update success.")));
                }
                else
                {
                    return(new OkObjectResult(ApiResponseModel.GetFailureModel("Update failure. Please try again.")));
                }
            }
            catch (Exception ex)
            {
                _log.LogInformation($"UPDATE IDNUMBER - ERROR. \n {ex.ToString()}");
                return(new BadRequestObjectResult(ApiResponseModel.GetErrorModel("Update error.")));
            }
        }
示例#2
0
        public async Task <IActionResult> UploadImage(List <IFormFile> files)
        {
            try
            {
                _log.LogInformation("UPLOAD DATA - START");
                DateTime now = DateTime.Now;
                if (files.Count == 0)
                {
                    _log.LogWarning("FILE IS EMPTY");
                    return(new OkObjectResult(ApiResponseModel.GetFailureModel("File is empty")));
                }
                else
                {
#if !DEBUG
                    if (!User.Identity.IsAuthenticated)
                    {
                        _log.LogWarning("USER NOT LOGIN");
                        return(new OkObjectResult(ApiResponseModel.GetFailureModel(ConstantsError.ERROR_USER_IS_NOT_LOGIN)));
                    }
#endif
                    var file     = files[0];
                    var fileName = file.GetFilename().Trim('"');

                    var imageFolder = $@"\images\{now.ToString("yyyyMMdd")}\{Guid.NewGuid().ToString()}";

                    string folder = _hostingEnvironment.WebRootPath + imageFolder;

                    if (!Directory.Exists(folder))
                    {
                        Directory.CreateDirectory(folder);
                    }
                    string filePath = Path.Combine(folder, fileName);

                    _log.LogInformation($"FILE PATH TO SAVE: {filePath}");
                    using (FileStream fs = System.IO.File.Create(filePath))
                    {
                        await file.CopyToAsync(fs);

                        fs.Flush();
                    }
                    _log.LogInformation("UPLOAD DATA SUCCESS. SAVE TO DB");

                    string urlImage = Path.Combine(imageFolder, fileName);
#if DEBUG
                    var userId = _userManager.FindByNameAsync("admin").Result.Id;
#else
                    var userId = User.GetSpecificClaim("UserId");
#endif

                    //Update AuthenticateImage and Status
                    if (!_appUserService.UpdateUrlImage(userId, urlImage))
                    {
                        _log.LogInformation("CAN NOT SAVE URL IMAGE TO DB.");
                        return(new OkObjectResult(ApiResponseModel.GetFailureModel("Save data error.")));
                    }

                    _log.LogInformation("UPLOAD DATA - END");

                    return(new OkObjectResult(ApiResponseModel.GetSuccessModel(urlImage.Replace(@"\", @"/"))));
                }
            }
            catch (Exception ex)
            {
                _log.LogError($"UPLOAD DATA ERROR. \n {ex.ToString()}");
                return(new OkObjectResult(ApiResponseModel.GetErrorModel("Upload file error. Please try again!")));
            }
        }
示例#3
0
        public async Task <IActionResult> ConfirmPhoneVerify(string phoneNumber, string code)
        {
            try
            {
                _log.LogInformation("CONFIRM PHONE VERIFY - START");
                if (string.IsNullOrEmpty(code))
                {
                    _log.LogInformation($"PHONE VERIFY CODE USER INPUT INVALID. USER INPUT: {code}");
                    ViewBag.VerifyError = ConstantsError.ERROR_PHONE_VERIFY_INVALID;
                    return(new OkObjectResult(ApiResponseModel.GetFailureModel(ConstantsError.ERROR_PHONE_VERIFY_INVALID)));
                }

#if DEBUG
                var user = _userManager.FindByNameAsync("admin").Result;
#else
                var user = await _userManager.GetUserAsync(HttpContext.User).Result;
#endif
                var verifyCode = user.PhoneOTP;

                if (string.IsNullOrEmpty(verifyCode) || code != verifyCode || string.IsNullOrEmpty(phoneNumber) || user.PhoneNumber != phoneNumber)
                {
                    _log.LogInformation($"PHONE VERIFY CODE USER INPUT INVALID. USER INPUT: {code}");
                    ViewBag.VerifyError = ConstantsError.ERROR_PHONE_VERIFY_INVALID;
                    return(new OkObjectResult(ApiResponseModel.GetFailureModel(ConstantsError.ERROR_PHONE_VERIFY_INVALID)));
                }

                if (user.OTPTime.AddMinutes(AppConfigConstants.USER_PHONE_VERIFY_TIMEOUT) < DateTime.Now)
                {
                    //OTP hết hạn
                    ViewBag.VerifyError = ConstantsError.ERROR_PHONE_VERIFY_TIMEOUT;
                    return(new OkObjectResult(ApiResponseModel.GetFailureModel(ConstantsError.ERROR_PHONE_VERIFY_TIMEOUT)));
                }

                if (code == verifyCode)
                {
                    _log.LogInformation("VERIFY CODE VALID. UPDATING USER INFO");
                    //Mã xác nhận đúng=> Thực hiện lưu sđt và cập nhật trạng thái số điện thoại đã xác nhận vào database
                    user.PhoneOTP             = string.Empty;
                    user.PhoneNumberConfirmed = true;

                    //Cập nhật thông tin User
                    await _userManager.UpdateAsync(user);

                    _log.LogInformation("CONFIRM PHONE VERIFY - SUCCESS");
                    _log.LogInformation("CONFIRM PHONE VERIFY - END");
                    return(new OkObjectResult(ApiResponseModel.GetSuccessModel("Phone confirm success")));
                }
                else
                {
                    _log.LogInformation($"PHONE VERIFY CODE USER INPUT INVALID. USER INPUT: {code}");
                    ViewBag.VerifyError = ConstantsError.ERROR_PHONE_VERIFY_INVALID;
                    _log.LogInformation("CONFIRM PHONE VERIFY - END");
                    return(new OkObjectResult(ApiResponseModel.GetFailureModel(ConstantsError.ERROR_PHONE_VERIFY_INVALID)));
                }
            }
            catch (Exception ex)
            {
                _log.LogInformation($"CONFIRM PHONE VERIFY ERROR. - {ex.ToString()}");
                return(new OkObjectResult(ApiResponseModel.GetErrorModel(ConstantsError.ERROR_PHONE_VERIFY_ERROR)));
            }
        }
示例#4
0
        public async Task <IActionResult> PhoneVerify(string phoneNumber)
        {
            try
            {
                _log.LogInformation("PHONE VERIFY - START");
                if (string.IsNullOrEmpty(phoneNumber))
                {
                    return(new OkObjectResult(ApiResponseModel.GetFailureModel(ConstantsError.ERROR_PHONE_INVALID)));
                }
#if !DEBUG
                //Kiểm tra user đã đăng nhập hay chưa
                if (!User.Identity.IsAuthenticated)
                {
                    return(new OkObjectResult(ApiResponseModel.GetFailureModel(ConstantsError.ERROR_USER_IS_NOT_LOGIN)));
                }
#endif
                //Lấy thông tin user đang đăng nhập
                _log.LogInformation("GET USER INFOMATION");

#if DEBUG
                var user = _userManager.FindByNameAsync("admin").Result;
#else
                var user = await _userManager.GetUserAsync(HttpContext.User).Result;
#endif
                if (!user.PhoneNumberConfirmed)
                {
                    //Tạo 1 code để gửi User verify
                    string verifyCode = new Random().Next(100000, 999999).ToString();
#if DEBUG
                    verifyCode = "999999";
#endif
                    //Build nội dung tin nhắn gửi cho User
                    string content = string.Format(AppConfigConstants.USER_VEIRFY_PHONE_CONTENT, verifyCode);

                    //Gửi tin nhắn mã số xác nhận sđt cho user
                    _log.LogInformation($"BEGIN SEND SMS TO USER. PHONE NUMBER IS: {phoneNumber}");
                    if (_smsHelper.SendSms(phoneNumber, content))
                    {
                        //_cache.Set<string>(phoneNumber, verifyCode, TimeSpan.FromMinutes(15));
                        user.PhoneNumber = phoneNumber;
                        user.PhoneOTP    = verifyCode;
                        user.OTPTime     = DateTime.Now;

                        //Cập nhật số OTP hiện tại
                        await _userManager.UpdateAsync(user);

                        ViewBag.PhoneNumber = phoneNumber;
                        _log.LogInformation("SEND VERIFY CODE AND SAVE VERIFY CODE AND PHONE NUMBER SUCCESS. - END");
                        return(new OkObjectResult(ApiResponseModel.GetSuccessModel("Success")));
                    }
                    else
                    {
                        return(new OkObjectResult(ApiResponseModel.GetFailureModel(ConstantsError.ERROR_PHONE_VERIFY_CANNOTSEND)));
                    }
                }
                else
                {
                    return(new OkObjectResult(ApiResponseModel.GetFailureModel(ConstantsError.ERROR_NOT_FOUND)));
                }
            }
            catch (Exception ex)
            {
                _log.LogInformation($"SEND SMS TO VERIFY PHONE ERROR. \n {ex.ToString()}");
                return(new OkObjectResult(ApiResponseModel.GetErrorModel(ConstantsError.ERROR_PHONE_VERIFY_ERROR)));
            }
        }