Exemplo n.º 1
0
        public IActionResult Post(SignupBindingModel model)
        {
            bool isValid = true;
            ErrorsSignupBindingModel errors = new ErrorsSignupBindingModel();

            if (string.IsNullOrEmpty(model.UserName))
            {
                errors.username = "******";
                isValid         = false;
            }
            if (!isValid)
            {
                return(BadRequest(new { errors, isValid }));
            }

            return(Ok(new { success = true }));
            //return View();
        }
Exemplo n.º 2
0
        public IHttpActionResult /*HttpResponseMessage*/ Post(SignupBindingModel model)
        {
            bool isValid = true;
            ErrorsSignupBindingModel errors = new ErrorsSignupBindingModel();

            if (string.IsNullOrEmpty(model.UserName))
            {
                errors.username = "******";
                isValid         = false;
            }
            if (string.IsNullOrEmpty(model.Email))
            {
                errors.email = "This field is required";
                isValid      = false;
            }
            try
            {
                MailAddress emailAddress = new MailAddress(model.Email);
            }
            catch
            {
                errors.email = "Email is invalid";
                isValid      = false;
            }
            if (string.IsNullOrEmpty(model.Password))
            {
                errors.password = "******";
                isValid         = false;
            }
            if (!model.Password.Equals(model.PasswordConfirmation))
            {
                errors.passwordConfirmation = "Passwords must match";
                isValid = false;
            }
            if (string.IsNullOrEmpty(model.Timezone))
            {
                errors.timezone = "This field is required";
                isValid         = false;
            }
            SignupResultModel result = new SignupResultModel
            {
                errors  = errors,
                isValid = isValid
            };

            if (!isValid)
            {
                return(Content(HttpStatusCode.BadRequest, result));
            }
            //return Request.CreateResponse(HttpStatusCode.BadRequest, result);

            HttpClient client     = new HttpClient();
            string     jsonObject = JsonConvert.SerializeObject(new
            {
                Email           = model.Email,
                Password        = model.Password,
                ConfirmPassword = model.Password
            });

            var request = HttpContext.Current.Request;
            var url     = request.Url.GetLeftPart(UriPartial.Authority) +
                          request.ApplicationPath + "/api/Account/Register";
            var content  = new StringContent(jsonObject, Encoding.UTF8, "application/json");
            var response = client.PostAsync(url, content).Result;

            if (response.StatusCode != System.Net.HttpStatusCode.OK)
            {
                return(Content(HttpStatusCode.BadRequest, result));
            }

            ICryptoService cryptoService = new PBKDF2();
            //save this salt to the database
            string salt = cryptoService.GenerateSalt();
            //save this hash to the database
            string hashedPassword = cryptoService.Compute(model.Password);

            SignupUser signup = new SignupUser
            {
                Email        = model.Email,
                Password     = hashedPassword,
                PasswordSalt = salt,
                Timezone     = model.Timezone,
                UserName     = model.UserName
            };

            try
            {
                using (ApplicationDbContext context = new ApplicationDbContext())
                {
                    context.SignupUser.Add(signup);
                    context.SaveChanges();
                    return(Content(HttpStatusCode.OK, new { success = true }));
                }
            }
            catch (Exception ex)
            {
                return(Content(HttpStatusCode.InternalServerError, new { error = ex }));
            }


            //return Request.CreateResponse(HttpStatusCode.OK, new { success=true });
        }