Exemple #1
0
        /// <summary>
        /// Register new customer
        /// </summary>
        /// <param name="user"></param>
        public async Task Register(SignUpPersonal user)
        {
            try
            {
                if (user == null)
                {
                    throw new ApplicationException("Incomplete register request - user is null");
                }
                if (user.EmailAddress == null)
                {
                    throw new ApplicationException("Incomplete register request - user's email is null");
                }
                if (user.Password == null || user.Password.Length == 0)
                {
                    throw new ApplicationException("Incomplete register request - Password is null");
                }
                var existingUser = _userRepository.Get(x => x.Login.Username == user.EmailAddress).FirstOrDefault();
                if (existingUser != null)
                {
                    throw new ApplicationException("Email address has been used in registration.");
                }

                // hash password
                var passHash = _encryptPassword.CreateHash(user.Password);

                //var passHash = new PBKDF2(user.Password,SALT_BYTES,PBKDF2_ITERATIONS,"HMACSHA512");
                var UId      = Guid.NewGuid();
                var objectId = ObjectId.GenerateNewId().ToString();
                var login    = new Login()
                {
                    Id                     = objectId,
                    UId                    = UId,
                    Username               = user.EmailAddress,
                    PasswordHash           = passHash,
                    IsDisabled             = true,
                    EmailAddressAuthorized = false,
                    EmailCode              = user.EmailCode,
                    ExpiredOn              = DateTime.UtcNow.AddHours(24),
                    PasswordFormat         = PBKDF2_ITERATIONS,
                    TermsAccepted          = user.TermsConditionsAccepted
                };

                var person = new User()
                {
                    Id          = objectId,
                    FirstName   = user.FirstName,
                    LastName    = user.LastName,
                    MobilePhone = user.MobileNumber,
                    CreatedOn   = DateTime.UtcNow,
                    IsDeleted   = false,
                    UId         = UId,
                    Login       = login,
                };

                await _userRepository.Add(person);
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Register error - " + ex.Message);
            }
        }
        public ActionResult Register(RegisterModel model)
        {
            try
            {
                model.RecaptchaSiteKey = SiteUtil.RecaptchaSiteKey;

                //if (!IsValidateReCaptcha())
                //{
                //    ModelState.AddModelError("", "Google reCaptcha validation failed.");
                //    return View(model);
                //}

                if (ModelState.IsValid)
                {
                    // Check password format
                    //if (!ValidatePassword(model.Password))
                    //{
                    //    ModelState.AddModelError("Password", "The password is not correct format");

                    //    return Json(new { Success = false, Message = "The password is not correct format" });
                    //}

                    if (_loginRepository.GetQueryable().Any(n => n.Username == model.Username))
                    {
                        //ModelState.AddModelError("", "Email has been used, " +
                        //                        "please click forget password to reset your password");
                        return(Json(new { Success = false, Message = "Email has been used, please click forget password to reset your password" }));
                    }
                    //Disable mobile number for now
                    var user = new SignUpPersonal()
                    {
                        FirstName               = model.FirstName,
                        LastName                = model.LastName,
                        MobileNumber            = "",
                        EmailAddress            = model.Username,
                        Password                = model.Password,
                        CountryCode             = "",
                        DialCode                = "",
                        EmailCode               = Guid.NewGuid(),
                        TermsConditionsAccepted = true
                    };

                    // Save to database
                    _signUpService.Register(user);

                    // Send a verification email to user
                    _emailService.SendMail(SiteUtil.WebsiteURL, SiteUtil.GmailAddress, model.Username,
                                           user.EmailCode, SiteUtil.EmailVerificationURL, SiteUtil.SenderName);

                    // Send a new register notification to support
                    _emailService.SendNewRegisterNotification(SiteUtil.SupportEmail, user);

                    return(Json(new { Success = true }));
                }
                else
                {
                    //ModelState.AddModelError("", "Data is not correct");
                    return(Json(new { Success = false, Message = "Data is not correct" }));
                }
            }
            catch (Exception x)
            {
                Logging.Error(x.Message, x);
                ModelState.AddModelError("", "Something went wrong, " +
                                         "our team has been notified with this error.");
            }
            return(Json(new { Success = false, Message = "Something went wrong, " +
                                                         "our team has been notified with this error." }));
        }