Ejemplo n.º 1
0
        public HttpResponseMessage IsReCaptchValid([FromBody] reCaptcha re)
        {
            string result = "";
            var    resp   = new reCaptchaResponse();

            try
            {
                if (re.token == "")
                {
                    return(Request.CreateResponse(HttpStatusCode.NoContent));
                }

                var responseKey = re.token;
                var secretKey   = Project.PrivateKey;
                var apiUrl      = "https://www.google.com/recaptcha/api/siteverify?secret={0}&response={1}";
                var requestUri  = string.Format(apiUrl, secretKey, responseKey);

                var request = (HttpWebRequest)WebRequest.Create(requestUri);

                using (WebResponse response = request.GetResponse()) {
                    using (StreamReader stream = new StreamReader(response.GetResponseStream())) {
                        resp = JsonConvert.DeserializeObject <reCaptchaResponse>(stream.ReadToEnd());
                    }
                }

                Msg = Request.CreateResponse(HttpStatusCode.OK, resp);
            } catch (Exception ex) {
                Project.GetErrorMessage(ex.Message);
                Msg = Request.CreateResponse(HttpStatusCode.NotFound);
            }
            return(Msg);
        }
Ejemplo n.º 2
0
        public async Task <IHttpActionResult> Register(RegisterBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            reCaptcha captcha = new reCaptcha();
            bool      captchaVerificiation = captcha.VerifyCaptcha(model.Captcha);

            if (!captchaVerificiation)
            {
                return(BadRequest("The user is identified as a bot."));
            }

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

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

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

            return(Ok());
        }
Ejemplo n.º 3
0
        public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            var       formData             = context.Request.ReadFormAsync().Result;
            string    captchaResponse      = formData.Get("captcha");
            reCaptcha captcha              = new reCaptcha();
            bool      captchaVerificiation = captcha.VerifyCaptcha(captchaResponse);

            if (!captchaVerificiation)
            {
                context.SetError("invalid_captcha", "The user was identified as a bot.");
                return;
            }

            var userManager = context.OwinContext.GetUserManager <ApplicationUserManager>();

            ApplicationUser user = await userManager.FindAsync(context.UserName, context.Password);

            if (user == null)
            {
                context.SetError("invalid_grant", "The user name or password is incorrect.");
                return;
            }

            ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(userManager,
                                                                                OAuthDefaults.AuthenticationType);

            ClaimsIdentity cookiesIdentity = await user.GenerateUserIdentityAsync(userManager,
                                                                                  CookieAuthenticationDefaults.AuthenticationType);

            AuthenticationProperties properties = CreateProperties(user.UserName);
            AuthenticationTicket     ticket     = new AuthenticationTicket(oAuthIdentity, properties);

            context.Validated(ticket);
            context.Request.Context.Authentication.SignIn(cookiesIdentity);
        }
Ejemplo n.º 4
0
        public async Task <IHttpActionResult> Register(RegisterBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            string    captchaToken = model.CaptchaToken;
            reCaptcha captcha      = new reCaptcha();
            bool      isHuman      = captcha.Verify(captchaToken);

            if (!isHuman)
            {
                return(BadRequest("You are a Robot."));
            }

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

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

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

            return(Ok());
        }