Recaptcha in Angular JS

前端 未结 4 2137
甜味超标
甜味超标 2021-01-12 23:57

I am implementing recaptcha in Angular JS, I am using \"https://github.com/VividCortex/angular-recaptcha\" url as reference. I have referred the Usage section and followed t

4条回答
  •  情书的邮戳
    2021-01-13 00:36

    example that woked for me
    register.cshtml:

    app.js:

    var app = angular.module('myApp', ['ngRoute','vcRecaptcha']);
    

    controller.js:

    var ctrl = function ($rootScope, $scope, $location, $routeParams, registrationService) {
    
            $scope.reCaptchaResponse = "";
            $scope.setResponse = function (response) {
                $scope.reCaptchaResponse = response;
            };
            $scope.register = function () {
                var registration = {
                                    ...
                    reCaptchaResponse: $scope.reCaptchaResponse
                }
                $http.post(serviceBase + 'Register', registration).then(function (results) {                
                    //return result
                });
            }
        }
    

    Controller.cs:

    [HttpPost]
        public JsonResult Register(UserDTO registration)
        {
            string responseFromServer = "";
            WebRequest request = WebRequest.Create("https://www.google.com/recaptcha/api/siteverify?secret=mysecret&response=" + registration.ReCaptchaResponse);
            request.Method = "GET";
            using (WebResponse response = request.GetResponse())
            {
                using (Stream stream = response.GetResponseStream())
                {
                    StreamReader reader = new StreamReader(stream);
                    responseFromServer = reader.ReadToEnd();
                }
            }
    
            if(responseFromServer != "")
            {
                bool isSuccess = false;          
                Dictionary values = JsonConvert.DeserializeObject>(responseFromServer);
                foreach (var item in values)
                {
                    if (item.Key == "success" && item.Value == "True")
                    {
                        isSuccess = true;
                        break;
                    }                        
                }
    
                if(isSuccess)
                {
                    //do something
                }
                else
                {
                    //return reCaptcha error
                }
    
            }
    
            return Json(result);
        }
    

提交回复
热议问题