Ejemplo n.º 1
0
        public async Task <IActionResult> Register(RegisterInputViewModel register)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(register));
            }

            if (await this.usersService.UsernameExists(register.Username))
            {
                this.ModelState.AddModelError(string.Empty, "Username already exists!");
                return(this.View(register));
            }

            if (await this.usersService.EmailExists(register.Email))
            {
                this.ModelState.AddModelError(string.Empty, "Email address already exists!");
                return(this.View(register));
            }

            var user = new ApplicationUser
            {
                UserName    = register.Username,
                Email       = register.Email,
                PhoneNumber = register.PhoneNumber,
            };

            await this.userManager.CreateAsync(user, register.Password);

            await this.usersService.AddUserInRole(user.Id);

            return(this.Redirect("/Users/Login"));
        }
        public HttpResponse Register(RegisterInputViewModel input)
        {
            if (string.IsNullOrWhiteSpace(input.Username) || input.Username.Length < 4 || input.Username.Length > 10)
            {
                return(this.Error("/Users/Register"));
            }

            if (string.IsNullOrWhiteSpace(input.Password) || input.Password.Length < 6 || input.Password.Length > 20)
            {
                return(this.Error("/Users/Register"));
            }

            if (string.IsNullOrWhiteSpace(input.Email))
            {
                return(this.Error("/Users/Register"));
            }

            if (input.Password != input.ConfirmPassword)
            {
                return(this.Error("/Users/Register"));
            }

            this.userService.Create(input);

            return(this.Redirect("/Users/Login"));
        }
        public HttpResponse Register(RegisterInputViewModel input)
        {
            if (string.IsNullOrEmpty(input.Username) || string.IsNullOrWhiteSpace(input.Username) || input.Username.Length < 5 || input.Username.Length > 20)
            {
                return(this.Error("Username is required and should be between 5 and 20 characters."));
            }

            if (!this.usersService.IsUsernameAvailable(input.Username))
            {
                return(this.Error("Username already taken."));
            }

            if (string.IsNullOrEmpty(input.Email) || !new EmailAddressAttribute().IsValid(input.Email))
            {
                return(this.Error("Invalid email address."));
            }

            if (!this.usersService.IsEmailAvailable(input.Email))
            {
                return(this.Error("Email already taken."));
            }

            if (string.IsNullOrEmpty(input.Password) || string.IsNullOrWhiteSpace(input.Password) || input.Password.Length < 6 || input.Password.Length > 20)
            {
                return(this.Error("Password should be between 6 and 20 charaters."));
            }

            if (input.Password != input.ConfirmPassword)
            {
                return(this.Error("Passwords do not match."));
            }

            this.usersService.CreateUser(input.Username, input.Email, input.Password);
            return(this.Redirect("/Users/Login"));
        }
Ejemplo n.º 4
0
        public async Task <ActionResult> Register(RegisterInputViewModel register)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.BadRequest(new { Message = "Ooopppsss! Something wrong!", this.ModelState, register }));
            }

            if (await this._userService.EmailExists(register.Email))
            {
                return(this.BadRequest(new { Message = "Email is already exists!", register }));
            }

            if (await this._userService.UsernameExists(register.Username))
            {
                return(this.BadRequest(new { Message = "Username is already exists!", register }));
            }

            var newUser = this._mapper.Map <ApplicationUser>(register);

            var data = await this._userManager.CreateAsync(newUser, register.Password);

            if (!data.Succeeded)
            {
                return(this.BadRequest(new { Message = "User is NOT create!", register }));
            }

            var user = this._mapper.Map <UserOutputViewModel>(newUser);

            return(this.Created("Login", new { Message = "User is created!", user }));
        }
        public HttpResponse Register(RegisterInputViewModel input)
        {
            if (input.Password.Length < 6 || input.Password.Length > 20)
            {
                return(this.View());
            }

            if (input.Username.Length < 5 || input.Username.Length > 20)
            {
                return(this.View());
            }

            if (input.Password != input.ConfirmPassword)
            {
                return(this.View());
            }

            if (this.usersService.EmailExists(input.Email))
            {
                return(this.View());
            }

            if (this.usersService.UsernameExists(input.Username))
            {
                return(this.View());
            }

            this.usersService.Register(input.Username, input.Email, input.Password);

            return(this.Redirect("/Users/Login"));
        }
        public HttpResponse Register(RegisterInputViewModel model)
        {
            if (model.Username.Length < 4 || model.Username.Length > 10)
            {
                return(this.Error("Username must be between 4 and 10 symbols!"));
            }
            if (model.Password.Length < 4 || model.Password.Length > 20)
            {
                return(this.Error("Password must be between 4 and 20 symbols!"));
            }
            if (model.Email.Length < 0)
            {
                return(this.Error("Invalid email"));
            }
            if (model.Password != model.ConfirmPassword)
            {
                return(this.Error("The passwords you entered do not match!"));
            }
            if (usersService.UsernameExists(model.Username))
            {
                return(this.Error("Username already exists!"));
            }
            if (usersService.EmailExists(model.Email))
            {
                return(this.Error("Email already exists!"));
            }
            usersService.AddUser(model.Username, model.Password, model.Email);

            return(this.Redirect("Login"));
        }
Ejemplo n.º 7
0
        public IActionResult Register(RegisterInputViewModel model)
        {
            if (model.Password != model.ConfirmPassword)
            {
                return(this.View());
            }

            var user = new User
            {
                Username = model.Username,
                Password = model.Password,
                Email    = model.Email,
                Role     = this.Context.Users.Any() ? UserRole.User : UserRole.Admin
            };

            SignIn(new IdentityUser
            {
                Username = user.Username,
                Email    = user.Email,
                Password = user.Password,
                Roles    = new List <string> {
                    user.Role.ToString()
                },
            });
            this.Context.Users.Add(user);
            this.Context.SaveChanges();

            return(RedirectToAction("/"));
        }
Ejemplo n.º 8
0
        public IActionResult Register(RegisterInputViewModel model)
        {
            var user = this.UserService.Register(model);

            this.SignIn(user);

            return(RedirectToAction("/"));
        }
Ejemplo n.º 9
0
        public void InvalidPassword(string password)
        {
            // Activate
            var validationPassword = new RegisterInputViewModel();

            // Act
            var result = validationPassword.Password = password;

            // Assert
            Assert.False(result.Length >= 6 && result.Length <= 30);
        }
Ejemplo n.º 10
0
        public void InvalidUsername(string username)
        {
            // Activate
            var validationUser = new RegisterInputViewModel();

            // Act
            var result = validationUser.Username = username;

            // Assert
            Assert.False(result.Length >= 5 && result.Length <= 30);
        }
Ejemplo n.º 11
0
        public IdentityUser Register(RegisterInputViewModel model)
        {
            var user = Mapper.Map <User>(model);

            this.Db.Users.Add(user);
            this.Db.SaveChanges();

            var result = CurrentUser(user);

            return(result);
        }
        public void Create(RegisterInputViewModel model)
        {
            var user = new User
            {
                Role     = SIS.MvcFramework.IdentityRole.User,
                Username = model.Username,
                Email    = model.Email,
                Password = this.Hash(model.Password)
            };

            this.db.Users.Add(user);
            this.db.SaveChanges();
        }
        public RegisterOutputViewModel Register(RegisterInputViewModel entity)
        {
            User result = new User()
            {
                FirstName = entity.FirstName,
                Surname   = entity.Surname,
                Password  = CryptologyFuncs.Hash(entity.Password),
                Email     = entity.Email
            };

            this._unitOfWork.User.Insert(result);
            this._unitOfWork.Save();
            return(_mapper.Map <RegisterOutputViewModel>(result));
        }
Ejemplo n.º 14
0
        public HttpResponse Register(RegisterInputViewModel input)
        {
            if (this.IsUserSignedIn())
            {
                return(this.Redirect("/Products/All"));
            }

            if (string.IsNullOrEmpty(input.Username) ||
                input.Username.Length < 3 ||
                input.Username.Length > 20)
            {
                return(this.Error("Invalid username."));
            }

            if (string.IsNullOrEmpty(input.Password) ||
                input.Password.Length < 6 ||
                input.Password.Length > 20)
            {
                return(this.Error("Invalid password."));
            }

            if (!this.usersService.IsUsernameAvailable(input.Username))
            {
                return(this.Error("Taken username."));
            }

            if (!Regex.IsMatch(input.Username, @"^[a-zA-Z0-9\.]+$"))
            {
                return(this.Error("Invalid username. Only alphanumeric characters are allowed."));
            }

            if (string.IsNullOrWhiteSpace(input.Email) || !new EmailAddressAttribute().IsValid(input.Email))
            {
                return(this.Error("Invalid email."));
            }

            if (!this.usersService.IsEmailAvailable(input.Email))
            {
                return(this.Error("Taken email."));
            }

            if (input.ConfirmPassword != input.Password)
            {
                return(this.Error("Passwords not same."));
            }

            this.usersService.CreateUser(input.Username, input.Email, input.Password);

            return(this.Redirect("/Users/Login"));
        }
        public async Task <IdentityResult> CreateUserAsync(RegisterInputViewModel registerInputViewModel)
        {
            var user = new WAGUser()
            {
                UserName    = registerInputViewModel.UserName,
                FirstName   = registerInputViewModel.FirstName,
                LastName    = registerInputViewModel.LastName,
                City        = registerInputViewModel.City,
                Email       = registerInputViewModel.Email,
                PhoneNumber = registerInputViewModel.PhoneNumber,
                Address     = registerInputViewModel.Address
            };

            return(await this.UserManager.CreateAsync(user, registerInputViewModel.Password));
        }
Ejemplo n.º 16
0
        public HttpResponse Register(RegisterInputViewModel input)
        {
            if (this.IsUserLoggedIn())
            {
                return(this.Redirect("/Trips/All"));
            }

            if (string.IsNullOrWhiteSpace(input.Username))
            {
                return(this.Redirect("/Users/Register"));
            }

            if (string.IsNullOrWhiteSpace(input.Email))
            {
                return(this.Redirect("/Users/Register"));
            }

            if (input.Username.Length < 5 || input.Username.Length > 20)
            {
                return(this.Redirect("/Users/Register"));
            }

            if (input.Password.Length < 6 || input.Password.Length > 20)
            {
                return(this.Redirect("/Users/Register"));
            }

            if (input.Password != input.ConfirmPassword)
            {
                return(this.Redirect("/Users/Register"));
            }

            if (this.usersService.EmailExists(input.Email))
            {
                return(this.Redirect("/Users/Register"));
            }

            if (this.usersService.UsernameExists(input.Username))
            {
                return(this.Redirect("/Users/Register"));
            }

            this.usersService.Register(input.Username, input.Email, input.Password);
            return(this.Redirect("/Users/Login"));
        }
Ejemplo n.º 17
0
        public void TestWhatReturnUsersController()
        {
            var controller = new UsersController(new MockUsersService(),
                                                 null,
                                                 null);

            var newCorrectRegister = new RegisterInputViewModel();

            newCorrectRegister.Username       = "******";
            newCorrectRegister.Email          = "*****@*****.**";
            newCorrectRegister.Phone          = "0877711475";
            newCorrectRegister.Password       = "******";
            newCorrectRegister.RepeatPassword = "******";

            var result = controller.Register(newCorrectRegister);

            Assert.True(result.IsCompleted);
        }
        public IActionResult Register(RegisterInputViewModel registerInputViewModel)
        {
            if (!ModelState.IsValid)
            {
                return(this.View(registerInputViewModel));
            }

            var registerResult = this.UserAccountService.CreateUserAsync(registerInputViewModel).Result;

            if (registerResult != IdentityResult.Success)
            {
                registerInputViewModel.UnsuccessfulRegistrationMessage = UnsuccessfulRegistrationMessage;

                return(this.View(registerInputViewModel));
            }

            return(RedirectToAction("Login", "UserAccount"));
        }
Ejemplo n.º 19
0
        public async Task <ActionResult> Register(RegisterInputViewModel register)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.BadRequest(new { Message = "Ooopppsss! Something wrong!", this.ModelState, register }));
            }

            if (await this.usersService.UsernameExists(register.Username))
            {
                return(this.BadRequest(new { Message = "Username is already exists!", register }));
            }

            if (await this.usersService.EmailExists(register.Email))
            {
                return(this.BadRequest(new { Message = "Email is already exists!", register }));
            }

            var newUser = new ApplicationUser
            {
                UserName    = register.Username,
                Email       = register.Email,
                PhoneNumber = register.Phone.ToString(),
                CreatedOn   = DateTime.UtcNow,
            };

            var data = await this.userManager.CreateAsync(newUser, register.Password);

            var user = new UserViewModel
            {
                Username = register.Username,
                Email    = register.Email,
                Phone    = register.Phone,
            };

            if (!data.Succeeded)
            {
                return(this.BadRequest(new { Message = "User  is NOT create!", register }));
            }

            // SignInAsync is for auto sign in
            // await this.signInManager.SignInAsync(user, false);

            return(this.CreatedAtAction("Login", new { Message = "User is created!", user }));
        }
Ejemplo n.º 20
0
        public RegisterViewModel PostRegister(RegisterInputViewModel register)
        {
            try
            {
                string cellNumber = register.CellNumber;

                User existUser = IsUserExist(cellNumber);
                int  code      = 12345;
                if (existUser == null)
                {
                    User user = CreateUserObject(cellNumber);

                    UnitOfWork.UserRepository.Insert(user);

                    UnitOfWork.Save();

                    code = CreateActivationCode(user.Id);

                    UnitOfWork.Save();

                    sms.SendActivationCode(cellNumber, code);

                    return(CompleteJson(user, true, Resources.Messages.Register_Success, null, 0));
                }

                code = CreateActivationCode(existUser.Id);

                UnitOfWork.Save();

                sms.SendActivationCode(cellNumber, code);

                return(CompleteJson(existUser, true, Resources.Messages.Register_Success, null, 0));
            }
            catch (Exception e)
            {
                return(new RegisterViewModel()
                {
                    Result = null,
                    Status = status.ReturnStatus(100, "خطا در بازیابی اطلاعات", false)
                });
            }
        }
        public HttpResponse Register(RegisterInputViewModel model)
        {
            if (this.IsUserLoggedIn())
            {
                return(this.Redirect("/"));
            }

            if (model.Username.Length < 5 || model.Username.Length > 20)
            {
                return(this.Error("Username should be between 5 and 20 symbols!"));
            }

            if (model.Password.Length < 6 || model.Password.Length > 20)
            {
                return(this.Error("Password should be between 6 and 20 symbols!"));
            }

            if (model.Password != model.ConfirmPassword)
            {
                return(this.Error("Password and Confirm Password should be the same!"));
            }

            if (string.IsNullOrWhiteSpace(model.Email))
            {
                return(this.Error("Invalid Email!"));
            }

            if (this.usersService.IsUsernameExist(model.Username))
            {
                return(this.Error("Username is already used!"));
            }

            if (this.usersService.IsEmailExist(model.Email))
            {
                return(this.Error("Email is already used!"));
            }

            this.usersService.RegisterUser(model.Username, model.Email, model.Password);

            return(this.Redirect("/Users/Login"));
        }
        public IHttpResponse Register(RegisterInputViewModel model)
        {
            var role = Role.User;

            if (!this.Context.Users.Any())
            {
                role = Role.Admin;
            }

            var user = new User
            {
                Username = model.Username,
                Email    = model.Email,
                Password = this.hashService.Hash(model.Password),
                Role     = role
            };

            this.Context.Users.Add(user);
            this.Context.SaveChanges();

            return(this.Redirect("/Users/Login"));
        }
        public IActionResult Register(RegisterInputViewModel registerInputViewModel)
        {
            if (!ModelState.IsValid)
            {
                return(this.View(registerInputViewModel));
            }

            var registerResult = this.UserAccountService.CreateUserAsync(registerInputViewModel).Result;

            if (registerResult != IdentityResult.Success)
            {
                registerInputViewModel.UnsuccessfulRegistrationMessage = UnsuccessfulRegistrationMessage;

                return(this.View(registerInputViewModel));
            }

            var currUser = this.UserAccountService.GetUserByUserName(registerInputViewModel.UserName);

            this.UserAccountService.AddUserInRoleAsync(currUser, GlobalConstants.UserRole).GetAwaiter().GetResult();

            return(RedirectToAction("Login", "UserAccount"));
        }
        public async Task <IActionResult> Register(RegisterInputViewModel model)
        {
            // provjeravamo da li su validni input podaci
            if (ModelState.IsValid)
            {
                //pravimo objekat naše klase koju možeš u Models naći unutar Security projekta
                //nasa klasa nema svojih propertya vec ih nasljedjuje od Identity klase koja ima npr Username, Email, PhoneNumber i sl
                var newUser = new ApplicationUser()
                {
                    Email    = model.Email,
                    UserName = model.Username
                };

                //ovdje pravimo putem Identitya novog korisnika i ovdje drugi parametar je password
                //jer ova funkcija CreateAsync ce primit password, hashovat ga i spremit u bazu sa ostalim podacima
                var result = await _userManager.CreateAsync(newUser, model.Password);

                //ako je uspjesno spremljeno ovdje im kao dodjeljumemo rolu Guest
                //slozili smo se da nece se administratori moci registrovat tako da ce ovo uvijek ovako bit
                if (result.Succeeded)
                {
                    await _userManager.AddToRoleAsync(newUser, "Attendee");

                    return(Redirect("https://127.0.0.1:44330/"));
                }
                else
                {
                    foreach (var error in result.Errors)
                    {
                        ModelState.AddModelError(error.Code, error.Description);
                    }
                    return(View("Register", model));
                }

                // Ako je sve uspjelo treba vratit na View neki Uspjesna registracija ili redirectat na Homepage ?? veze nemam
            }
            // vracamo na registracioni View opet ako nesto nije bilo dobro I guess ? i ispisujemo greske
            return(View("Register", model));
        }
        public HttpResponse Register(RegisterInputViewModel model)
        {
            // Email empty; Email exists
            if (string.IsNullOrWhiteSpace(model.Email))
            {
                return(this.Error("Email cannot be empty."));
            }

            if (this.userService.EmailExists(model.Email))
            {
                return(this.Error("Email already in use."));
            }

            // Username string constraints; Username exists
            if (model.Username.Length < 4 || model.Username.Length > 10)
            {
                return(this.Error("Username must be between 4 and 10 characters long."));
            }

            if (this.userService.UsernameExists(model.Username))
            {
                return(this.Error("Username already in use."));
            }

            // Password string constraints; Passwords not matching
            if (model.Password.Length < 6 || model.Password.Length > 20)
            {
                return(this.Error("Password must be between 6 and 20 characters long."));
            }

            if (model.Password != model.ConfirmPassword)
            {
                return(this.Error("Passwords do not match."));
            }

            this.userService.Register(model.Username, model.Email, model.Password);

            return(this.Redirect("/Users/Login"));
        }
Ejemplo n.º 26
0
        public async Task <ActionResult> Register(RegisterInputViewModel register)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.BadRequest(this.ModelState));
            }

            if (await this.usersService.UsernameExists(register.Username))
            {
                return(this.BadRequest("Username is already exists!"));
            }

            if (await this.usersService.EmailExists(register.Email))
            {
                return(this.BadRequest("Email is already exists!"));
            }

            var user = new ApplicationUser
            {
                UserName    = register.Username,
                Email       = register.Email,
                PhoneNumber = register.Phone.ToString(),
                CreatedOn   = DateTime.UtcNow,
            };

            var newUser = await this.userManager.CreateAsync(user, register.Password);

            if (newUser.Succeeded)
            {
                await this.usersService.AddUserInRole(user.Id);

                return(this.CreatedAtAction("Login", new { Message = $"User {register.Username} is created!" }));
            }
            else
            {
                return(this.BadRequest(new { Message = $"User {register.Username} is NOT create!" }));
            }
        }
        public IActionResult Register(RegisterInputViewModel registerInputViewModel)
        {
            if (!ModelState.IsValid)
            {
                return(this.View(registerInputViewModel));
            }

            WAGUser userNew        = mapper.Map <WAGUser>(registerInputViewModel);
            var     registerResult = this.UserAccountService.CreateUserAsync(userNew, registerInputViewModel.Password).Result;

            if (!registerResult.Succeeded)
            {
                registerInputViewModel.UnsuccessfulRegistrationMessage = UnsuccessfulRegistrationMessage;

                return(this.View(registerInputViewModel));
            }

            var currUser = this.UserAccountService.GetUserByUserName(registerInputViewModel.UserName);

            this.UserAccountService.AddUserInRoleAsync(currUser, GlobalConstants.UserRole).GetAwaiter().GetResult();

            return(RedirectToAction("Login", "UserAccount"));
        }
Ejemplo n.º 28
0
        public HttpResponse Register(RegisterInputViewModel view)
        {
            if (!this.IsUserSignedIn())
            {
                return(this.Redirect("/"));
            }

            if (string.IsNullOrEmpty(view.Username) || view.Username.Length < 5 || view.Username.Length > 20)
            {
                return(this.Error("username must be bigger from 5 and not empty!"));
            }

            if (!Regex.IsMatch(view.Username, @"^[a-zA-Z0-9\.]+$"))
            {
                return(this.Error("Invalid username. Only alphanumeric characters are allowed."));
            }

            if (string.IsNullOrWhiteSpace(view.Email) || !new EmailAddressAttribute().IsValid(view.Email))
            {
                return(this.Error("Invalid email."));
            }

            if (view.Password == null || view.Password.Length < 6 || view.Password.Length > 20)
            {
                return(this.Error("Invalid password. The password should be between 6 and 20 characters."));
            }

            if (view.Password != view.ConfirmPassword)
            {
                return(this.Error("Passwords should be the same."));
            }

            this.userService.CreateUser(view.Username, view.Email, view.Password);

            return(this.Redirect("/Users/Login"));
        }
        public HttpResponse Register(RegisterInputViewModel model)
        {
            if (this.IsUserLoggedIn())
            {
                return(this.Redirect("/"));
            }

            if (model.Username.Length < 4 || model.Username.Length > 10)
            {
                return(this.View());
            }

            if (model.Password.Length < 6 || model.Password.Length > 20)
            {
                return(this.View());
            }

            if (model.Password != model.ConfirmPassword)
            {
                return(this.View());
            }

            if (this.usersService.IsUsernameExist(model.Username))
            {
                return(this.View());
            }

            if (this.usersService.IsEmailExist(model.Email))
            {
                return(this.View());
            }

            this.usersService.CreateUser(model.Username, model.Email, model.Password);

            return(this.View("Login"));
        }
Ejemplo n.º 30
0
        public HttpResponse Register(RegisterInputViewModel input)
        {
            if (this.IsUserSignedIn())
            {
                return(this.Redirect("/"));
            }

            if (input.Username == null || input.Username.Length < 4 || input.Username.Length > 20)
            {
                return(this.Error("Invalid username. The username should be between 5 and 20 characters."));
            }

            if (string.IsNullOrWhiteSpace(input.Email) || !new EmailAddressAttribute().IsValid(input.Email))
            {
                return(this.Error("Invalid email."));
            }

            if (input.Password == null || input.Password.Length < 5 || input.Password.Length > 20)
            {
                return(this.Error("Invalid password. The password should be between 6 and 20 characters."));
            }

            if (input.Password != input.ConfirmPassword)
            {
                return(this.Error("Passwords should be the same."));
            }

            if (this.usersService.IsUsernameAvailable(input.Username))
            {
                return(this.Error("Username already taken."));
            }

            this.usersService.Create(input.Username, input.Email, input.Password, input.UserType);

            return(this.Redirect("/Users/Login"));
        }