public void ValidateResponse_InvalidResponseWithNull()
        {
            var mock = new Mock <IRestClient>();

            mock.Setup(x => x.Execute(It.IsAny <IRestRequest>()))
            .Returns((IRestResponse)null);

            var client   = new ReCaptchaService(mock.Object);
            var response = client.ValidateResponse(TestKey, TestResponse, TestIpAddress);

            Assert.IsTrue(response == ReCaptchaValidationResult.InvalidResponse);
        }
        public void ValidateResponse_SuccessResponse()
        {
            var mock = new Mock <IRestClient>();

            mock.Setup(x => x.Execute(It.IsAny <IRestRequest>()))
            .Returns(new RestResponse {
                Content = "{success:true}"
            });

            var client   = new ReCaptchaService(mock.Object);
            var response = client.ValidateResponse(TestKey, TestResponse, TestIpAddress);

            Assert.IsTrue(response == ReCaptchaValidationResult.Success);
        }
        public async Task <IHttpActionResult> Register(RegisterBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            // Validate Recaptcha
            var reCaptchaVerfiyUrl = ConfigurationManager.AppSettings["ReCaptchaVerfiyUrl"];
            var reCaptchaSecretKey = ConfigurationManager.AppSettings["ReCaptchaSecretKey"];

            if (!string.IsNullOrEmpty(reCaptchaVerfiyUrl) && !string.IsNullOrEmpty(reCaptchaSecretKey))
            {
                var remoteIpAddress  = Request.GetOwinContext().Request.RemoteIpAddress;
                var reCaptchaService = new ReCaptchaService(new RestClient(reCaptchaVerfiyUrl));

                var validationResults = reCaptchaService.ValidateResponse(reCaptchaSecretKey, model.ReCaptchaResponse, remoteIpAddress);
                if (validationResults != ReCaptchaValidationResult.Disabled && validationResults != ReCaptchaValidationResult.Success)
                {
                    ModelState.AddModelError("ReCaptchaResponse", new Exception("Unable to validate reCaptcha Response"));
                    return(BadRequest(ModelState));
                }
            }

            // Add User
            var user = new ApplicationUser()
            {
                UserName = model.Email, Email = model.Email
            };

            user.Organizations.Add(new OrganizationMembership {
                EIN = model.EIN, IsAdmin = true
            });

            IdentityResult result = await UserManager.CreateAsync(user, model.Password);

            if (!result.Succeeded)
            {
                return(GetErrorResult(result));
            }

            return(Ok());
        }
        public async Task <IHttpActionResult> Register(RegisterViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            // Validate Recaptcha
            var reCaptchaVerfiyUrl = AppSettings.Get <string>("ReCaptchaVerfiyUrl");
            var reCaptchaSecretKey = AppSettings.Get <string>("ReCaptchaSecretKey");

            if (!string.IsNullOrEmpty(reCaptchaVerfiyUrl) && !string.IsNullOrEmpty(reCaptchaSecretKey))
            {
                var remoteIpAddress  = Request.GetOwinContext().Request.RemoteIpAddress;
                var reCaptchaService = new ReCaptchaService(new RestClient(reCaptchaVerfiyUrl));

                var validationResults = reCaptchaService.ValidateResponse(reCaptchaSecretKey, model.ReCaptchaResponse, remoteIpAddress);
                if (validationResults != ReCaptchaValidationResult.Disabled && validationResults != ReCaptchaValidationResult.Success)
                {
                    ModelState.AddModelError("ReCaptchaResponse", new Exception("Unable to validate reCaptcha Response"));
                    return(BadRequest(ModelState));
                }
            }

            // Add User
            var now  = DateTime.UtcNow;
            var user = new ApplicationUser()
            {
                UserName = model.Email, Email = model.Email, EmailConfirmed = false
            };

            user.Organizations.Add(new OrganizationMembership {
                EIN = model.EIN, IsAdmin = true, CreatedAt = now, LastModifiedAt = now, CreatedBy_Id = user.Id, LastModifiedBy_Id = user.Id
            });

            IdentityResult result = await UserManager.CreateAsync(user, model.Password);

            if (!result.Succeeded)
            {
                return(GetErrorResult(result));
            }

            // Add to application role
            result = await UserManager.AddToRoleAsync(user.Id, Roles.Applicant);

            if (!result.Succeeded)
            {
                return(GetErrorResult(result));
            }

            // Send Verification Email
            var nounce = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);

            var queryString = HttpUtility.ParseQueryString(string.Empty);

            queryString["userId"] = user.Id;
            queryString["code"]   = nounce;

            //TODO: Support Urls with existing querystring
            var callbackUrl = $@"{model.EmailVerificationUrl}?{queryString}";

            await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account: " + callbackUrl);

            return(Ok());
        }