Exemplo n.º 1
0
        public async Task <IActionResult> Register(
            string userName, string password, [FromBody] CaptchaResponseWrapper captchaResponse)
        {
            await captchaResponse.ValidateRecaptcha(ModelState);

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (userName == null || password == null)
            {
                return(BadRequest());
            }

            var user = new ApplicationUser {
                UserName = userName
            };

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

            if (result.Succeeded)
            {
                await _signInManager.SignInAsync(user, isPersistent : false);

                _logger.LogInformation(3, "User created a new account with password.");
            }

            return(result.Succeeded ? Ok() : GetErrorResult(result));
        }
        public static async Task ValidateRecaptcha(this CaptchaResponseWrapper captchaResponse, ModelStateDictionary modelState)
        {
            var token = captchaResponse.CaptchaResponse;

            using (var webClient = new HttpClient())
            {
                var content = new FormUrlEncodedContent(new[]
                {
                    new KeyValuePair <string, string>("secret", ReCaptchaSecret),
                    new KeyValuePair <string, string>("response", token)
                }
                                                        );

                HttpResponseMessage response = await webClient.PostAsync(ApiVerificationEndpoint, content);

                string json = await response.Content.ReadAsStringAsync();

                var reCaptchaResponse = JsonConvert.DeserializeObject <ReCaptchaResponse>(json);

                if (reCaptchaResponse == null)
                {
                    modelState.AddModelError(ReCaptchaModelErrorKey, "Unable To Read Response From Server");
                }
                else if (!reCaptchaResponse.Success)
                {
                    modelState.AddModelError(ReCaptchaModelErrorKey, "Invalid reCaptcha");
                }
            }
        }
    public static async Task ValidateRecaptcha(string captchaResponse, ModelStateDictionary modelState)
    {
        var captcha = new CaptchaResponseWrapper()
        {
            CaptchaResponse = captchaResponse
        };

        await captcha.ValidateRecaptcha(modelState);
    }
Exemplo n.º 4
0
        public async Task <IActionResult> PostImage([FromRoute] string title, IFormFile imageFile, string captchaResponse)
        {
            await CaptchaResponseWrapper.ValidateRecaptcha(captchaResponse, ModelState);

            if (imageFile.Length > maximumImageSize)
            {
                ModelState.AddModelError("", String.Format("The uploaded image exceeds maximum size of {0}kB.", maximumImageSize / 1024));
            }

            if (!IsJpgFile(imageFile))
            {
                ModelState.AddModelError("", "Couldn't proccess uploaded file. Please upload a valid JPG file.");
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            using (Stream stream = imageFile.OpenReadStream())
            {
                using (var binaryReader = new BinaryReader(stream))
                {
                    var fileContent = binaryReader.ReadBytes((int)imageFile.Length);

                    var newImage = new Image
                    {
                        Title     = title,
                        ImageData = fileContent,
                        OwnerId   = this.User.GetUserId()
                    };

                    _context.Add(newImage);

                    await _context.SaveChangesAsync();

                    return(Ok());
                }
            }
        }