예제 #1
0
        public ActionResult Register(RegisterModel model, string returnUrl, FormCollection form)
        {
            if (_workContext.CurrentAccount == null || _workContext.CurrentAccount.IsRegistered())
            {
                //Already registered customer. 
                _authenticationService.SignOut();
              
                //Save a new record
               // _workContext.CurrentAccount = _accountService.InsertGuestAccount();
            }

            var account = new Account(){
                  Birthdate = model.Birthdate,
                  Contact = model.Contact,
                Country= model.Country,
                  Email = model.Email,
                   Deleted = false,
                   Gender = model.Gender,
                   Language = model.Language,
                   CreatedOnUtc = DateTime.Now,
                   LastActivityDateUtc = DateTime.Now,
                   AccountGuid = Guid.NewGuid()

                   
            };

            
            if (ModelState.IsValid)
            {
                 
                   var registrationRequest = new AccountRegistrationRequest(account, model.Email, model.Password, PasswordFormat.Hashed);
               // _accountService.InsertGuestAccount
                var registrationResult = _accountRegistrationService.RegisterAccount(registrationRequest);
                if (registrationResult.Success)
                { _accountService.CreateAccount(account);
                  
                    _authenticationService.SignIn(account, true);
                    return RedirectToRoute("HomePage");
                }
                else
                {
                    foreach (var error in registrationResult.Errors)
                        ModelState.AddModelError("", error);
                }
            }
            return View(model);
        }
        public AccountRegistrationResult RegisterAccount(AccountRegistrationRequest request)
        {
            if (request == null)
                throw new ArgumentNullException("request");

            if (request.Account == null)
                throw new ArgumentException("Can't load current account");

            var result = new AccountRegistrationResult();

            if (request.Account.IsRegistered())
            {
                result.AddError("Current account is already registered");
                return result;
            }
            if (String.IsNullOrEmpty(request.Email))
            {
                result.AddError("Email Is Not Provided");
                return result;
            }
            if (!CommonHelper.IsValidEmail(request.Email))
            {
                result.AddError("Common.WrongEmail");
                return result;
            }
            if (String.IsNullOrWhiteSpace(request.Password))
            {
                result.AddError("Account.Register.Errors.PasswordIsNotProvided");
                return result;
            }

            //validate unique user
            if (_accountService.GetAccountByEmail(request.Email) != null)
            {
                result.AddError("Account.Register.Errors.EmailAlreadyExists");
                return result;
            }

            //at this point request is valid
            request.Account.Email = request.Email;
            request.Account.PasswordFormat = request.PasswordFormat;

            switch (request.PasswordFormat)
            {
                case PasswordFormat.Clear:
                    {
                        request.Account.Password = request.Password;
                    }
                    break;
                case PasswordFormat.Encrypted:
                    {
                        request.Account.Password = _encryptionService.EncryptText(request.Password);
                    }
                    break;
                case PasswordFormat.Hashed:
                    {
                        string saltKey = _encryptionService.CreateSaltKey(5);
                        request.Account.PasswordSalt = saltKey;
                        request.Account.Password = _encryptionService.CreatePasswordHash(request.Password, saltKey);
                    }
                    break;
                default:
                    break;
            }

            request.Account.Active = true;

            //add to 'Registered' role
            var registeredRole = _accountService.GetAccountRoleBySystemName(SystemAccountRoleNames.Registered);
            if (registeredRole == null)
                throw new Exception("'Registered' role could not be loaded");
            request.Account.AccountRoles.Add(registeredRole);
            //remove from 'Guests' role
            var guestRole =
                request.Account.AccountRoles.FirstOrDefault(cr => cr.SystemName == SystemAccountRoleNames.Guests);
            if (guestRole != null)
                request.Account.AccountRoles.Remove(guestRole);

            _accountService.UpdateAccount(request.Account);
            return result;
        }
        public AccountRegistrationResult RegisterAccount(AccountRegistrationRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            if (request.Account == null)
            {
                throw new ArgumentException("Can't load current account");
            }

            var result = new AccountRegistrationResult();

            if (request.Account.IsRegistered())
            {
                result.AddError("Current account is already registered");
                return(result);
            }
            if (String.IsNullOrEmpty(request.Email))
            {
                result.AddError("Email Is Not Provided");
                return(result);
            }
            if (!CommonHelper.IsValidEmail(request.Email))
            {
                result.AddError("Common.WrongEmail");
                return(result);
            }
            if (String.IsNullOrWhiteSpace(request.Password))
            {
                result.AddError("Account.Register.Errors.PasswordIsNotProvided");
                return(result);
            }

            //validate unique user
            if (_accountService.GetAccountByEmail(request.Email) != null)
            {
                result.AddError("Account.Register.Errors.EmailAlreadyExists");
                return(result);
            }

            //at this point request is valid
            request.Account.Email          = request.Email;
            request.Account.PasswordFormat = request.PasswordFormat;

            switch (request.PasswordFormat)
            {
            case PasswordFormat.Clear:
            {
                request.Account.Password = request.Password;
            }
            break;

            case PasswordFormat.Encrypted:
            {
                request.Account.Password = _encryptionService.EncryptText(request.Password);
            }
            break;

            case PasswordFormat.Hashed:
            {
                string saltKey = _encryptionService.CreateSaltKey(5);
                request.Account.PasswordSalt = saltKey;
                request.Account.Password     = _encryptionService.CreatePasswordHash(request.Password, saltKey);
            }
            break;

            default:
                break;
            }

            request.Account.Active = true;

            //add to 'Registered' role
            var registeredRole = _accountService.GetAccountRoleBySystemName(SystemAccountRoleNames.Registered);

            if (registeredRole == null)
            {
                throw new Exception("'Registered' role could not be loaded");
            }
            request.Account.AccountRoles.Add(registeredRole);
            //remove from 'Guests' role
            var guestRole =
                request.Account.AccountRoles.FirstOrDefault(cr => cr.SystemName == SystemAccountRoleNames.Guests);

            if (guestRole != null)
            {
                request.Account.AccountRoles.Remove(guestRole);
            }

            _accountService.UpdateAccount(request.Account);
            return(result);
        }