public IActionResult Register([FromBody] RegisterViewModel model)
 {
     if (!CaptchaHelper.VerifyAndExpireSolution(this.HttpContext, model.CaptchaKey,
                                                model.CaptchaText))
     {
         var invalid = new Dictionary <string, string>();
         invalid.Add("captchaText", "Помилка вводу зображення на фото");
         return(BadRequest(invalid));
     }
     return(Ok());
 }
 public ActionResult SubmitRegistration(string myCaptcha, string attempt)
 {
     if (CaptchaHelper.VerifyAndExpireSolution(HttpContext, myCaptcha, attempt))
     {
         // In a real app, actually register the user now
         return(Content("Pass"));
     }
     else
     {
         // In a real app, redisplay the view with an error message
         return(Content("Fail"));
     }
 }
Example #3
0
        public async Task <IActionResult> Register([FromBody] CustomRegisterModel model)
        {
            if (!ModelState.IsValid)
            {
                var errors = CustomValidator.GetErrorsByModel(ModelState);
                return(BadRequest(errors));
            }

            if (!CaptchaHelper.VerifyAndExpireSolution(this.HttpContext, model.CaptchaKey,
                                                       model.CaptchaText))
            {
                var invalid = new Dictionary <string, string>();
                invalid.Add("captchaText", "Error captcha text. Renew captcha and repeat again.");
                return(BadRequest(invalid));
            }

            string path = _fileService.UploadImage(model.ImageBase64);

            var user = new DbUser
            {
                UserName    = model.Email,
                Email       = model.Email,
                DateOfBirth = model.DateOfBirth,
                FirstName   = model.FirstName,
                MiddleName  = model.MiddleName,
                LastName    = model.LastName,
                SignUpTime  = DateTime.Now,
                AvatarUrl   = path
            };

            var result = await _userManager
                         .CreateAsync(user, model.Password);

            if (!result.Succeeded)
            {
                var errors = CustomValidator.GetErrorsByIdentityResult(result);
                return(BadRequest(errors));
            }
            var roleName = "User";

            var roleresult = _roleManager.CreateAsync(new DbRole
            {
                Name = roleName
            }).Result;

            result = _userManager.AddToRoleAsync(user, roleName).Result;

            await _signInManager.SignInAsync(user, isPersistent : false);

            return(Ok());
        }
 public ActionResult SubmitRegistration(string myCaptcha, string attempt)
 {
     if (CaptchaHelper.VerifyAndExpireSolution(HttpContext, myCaptcha, attempt))
     {
         // In a real app, actually register the user now
         return(Content("Pass"));
     }
     else
     {
         // Redisplay the view with an error message
         ModelState.AddModelError("attempt", "Incorrect - please try again");
         return(View("Index"));
     }
 }
Example #5
0
 public ActionResult QuenMatKhau(string email, string myCaptcha, string attempt)
 {
     if (CaptchaHelper.VerifyAndExpireSolution(HttpContext, myCaptcha, attempt))
     {
         ViewData["KiemTraCaptcha"] = true;
         orderSubmitter.SendEmail(email);
         return(View("LayMatKhauThanhCong"));
     }
     else
     {
         ViewData["KiemTraCaptcha"] = false;
         return(View());
     }
 }
Example #6
0
        public ActionResult QuenMatKhau(string email, string myCaptcha, string attempt)
        {
            // Kiểm tra Captcha

            if (CaptchaHelper.VerifyAndExpireSolution(HttpContext, myCaptcha, attempt))
            {
                // In a real app, actually register the user now
                ViewData["KiemTraCaptcha"] = true;
                orderSubmitter.SendEmail(email);
                return(View("LayMatKhauThanhCong"));
            }
            else
            {
                // In a real app, redisplay the view with an error message

                ViewData["KiemTraCaptcha"] = false;
                return(View());
            }
        }
        public async Task <IActionResult> RegisterUser([FromBody] RegisterDTO model)
        {
            // Auto return errors from viewModel and other global errors
            return(await HandleRequestAsync(async() =>
            {
                if (!CaptchaHelper.VerifyAndExpireSolution(this.HttpContext,
                                                           model.CaptchaKey,
                                                           model.CaptchaText))
                {
                    var invalid = new Dictionary <string, string>();
                    invalid.Add("captchaText", "Помилка вводу зображення на фото");
                    return BadRequest(invalid);
                }
                var user = _userManager.FindByEmailAsync(model.Email).Result;
                if (user != null)
                {
                    var invalid = new Dictionary <string, string>();
                    invalid.Add("email", "Користувач з даною електронною поштою уже зареєстрований");
                    return BadRequest(invalid);
                }

                string imageName = Path.GetRandomFileName() + ".jpg";

                try
                {
                    this._logger.LogDebug("Start register method RegisterUser...");
                    string pathSaveImages = InitStaticFiles
                                            .CreateImageByFileName(_env, _configuration,
                                                                   new string[] { "ImagesPath", "ImagesPathUser" },
                                                                   imageName,
                                                                   model.Photo);
                    this._logger.LogDebug("Save image complete method RegisterUser...");
                    if (pathSaveImages != null)
                    {
                        var profile = new UserProfile()
                        {
                            RegistrationDate = DateTime.Now,
                            FirstName = model.FirstName,
                            LastName = model.LastName,
                            Photo = imageName,
                        };
                        user = new DbUser()
                        {
                            UserName = model.Email,
                            Email = model.Email,
                            PhoneNumber = model.Phone,
                            UserProfile = profile
                        };
                        var result = _userManager.CreateAsync(user, model.Password).Result;
                        if (!result.Succeeded)
                        {
                            var errors = CustomValidator.GetErrorsByIdentityResult(result);
                            return BadRequest(errors);
                        }
                        await _signInManager.SignInAsync(user, isPersistent: false);
                        // Return token
                        JwtInfo jwtInfo = new JwtInfo()
                        {
                            Token = _jwtTokenService.CreateToken(user),
                            RefreshToken = _jwtTokenService.CreateRefreshToken(user)
                        };

                        this._logger.LogDebug("End method RegisterUser...");

                        return Ok(jwtInfo);
                    }
                    else
                    {
                        throw new Exception("Помила додавання фото в БД");
                    }
                }
                catch (Exception ex)
                {
                    InitStaticFiles.DeleteImageByFileName(_env, _configuration,
                                                          new string[] { "ImagesPath", "ImagesPathUser" },
                                                          imageName);
                    var errors = new Dictionary <string, string>();
                    errors.Add("invalid", ex.Message);
                    return BadRequest(errors);
                }
            }));
        }