public async Task <IActionResult> Register(UserRegisterBindingModel model) { if (ModelState.IsValid) { IdentityUser user = new IdentityUser { Email = model.Email, UserName = model.Username }; IdentityResult result = await this.userManager.CreateAsync(user, model.Password); if (result.Succeeded) { await this.signInManager.SignInAsync(user, isPersistent : false); return(RedirectToAction("Index", "Home")); } foreach (var error in result.Errors) { ModelState.AddModelError("", error.Description); } } return(View(model)); }
public IActionResult Register(UserRegisterBindingModel model) { if (!this.ModelState.IsValid) { return(this.Redirect("/Users/Register")); } if (model.Password != model.ConfirmPassword) { return(this.Redirect("/Users/Register")); } User user = new User { Username = model.Username, Password = this.HashPassword(model.Password), Email = model.Email }; this.userService.CreateUser(user); this.orderService.CreateOrder(new Order { CashierId = user.Id }); return(this.Redirect("/Users/Login")); }
private void ShowError(UserRegisterBindingModel model) { if (string.IsNullOrWhiteSpace(model.Username)) { this.Model.Data["error"] = "Username is required"; } else if (string.IsNullOrWhiteSpace(model.Email)) { this.Model.Data["error"] = "Email is required"; } else if (string.IsNullOrWhiteSpace(model.Password)) { this.Model.Data["error"] = "Password is required"; } else if (string.IsNullOrWhiteSpace(model.ConfirmPassword)) { this.Model.Data["error"] = "Confirm password fild is required"; } else if (model.Username.Length < 5) { this.Model.Data["error"] = "Username must be at least 5 cheracters long"; } else if (model.Password.Length < 6) { this.Model.Data["error"] = "Password must be at least 6 cheracters long"; } else if (model.Password != model.ConfirmPassword) { this.Model.Data["error"] = "Password do not match"; } }
public HashSet <RegistrationErrorVM> ValidateUserRegister(UserRegisterBindingModel urbm) { HashSet <RegistrationErrorVM> revms = new HashSet <RegistrationErrorVM>(); if (urbm.Username.Length < 5 || urbm.Username.Length > 30) { revms.Add(new RegistrationErrorVM(Constants.UserNameLengthErrorMessage)); } if (urbm.FullName.Length < 5) { revms.Add(new RegistrationErrorVM(Constants.FullNameTooShortMessage)); } Regex specialSymbolrgx = new Regex(@"[!@#$%^&*,.]"); if (urbm.Password.Length < 8 || !urbm.Password.Any(char.IsUpper) || !urbm.Password.Any(char.IsDigit) || !specialSymbolrgx.IsMatch(urbm.Password)) { revms.Add(new RegistrationErrorVM(Constants.PasswordIncorrectFormatMessage)); } if (urbm.Password != urbm.ConfirmPassword) { revms.Add(new RegistrationErrorVM(Constants.PasswordsDoNotMatchMessage)); } return(revms); }
public IActionResult Register(HttpResponse response, UserRegisterBindingModel urbm) { // validations? if (urbm.Password == urbm.ConfirmPassword && urbm.Email.Contains("@")) { var user = new User() { Username = urbm.Username, Email = urbm.Email, Password = urbm.Password, IsAdmin = !this.data.Users.GetAll().Any(), }; try { this.data.Users.Insert(user); this.data.SaveChanges(); } catch (Exception e) { return(this.View()); } this.Redirect(response, "/forum/login"); return(null); } return(this.View()); }
public bool HasRegister(UserRegisterBindingModel userBindingModel) { if (userBindingModel == null || !userBindingModel.IsValid()) { return(false); } if (this.userRepository.GetAll().Any(u => u.Email == userBindingModel.Email)) { return(false); } User user = new User { Email = userBindingModel.Email, Password = userBindingModel.Password, FullName = userBindingModel.Name, IsAdministrator = !this.userRepository.GetAll().Any() }; this.userRepository.Insert(user); this.unitOfWork.Save(); return(true); }
public IActionResult Register(UserRegisterBindingModel userRegisterModel) { if (this.IsLoggedIn()) { return(Redirect("/")); } if (!this.ModelState.IsValid) { return(this.Redirect("/Users/Register")); } if (this.userService .AnyUserWithTheGivenUsernameOrEmail(userRegisterModel.Username, userRegisterModel.Email) || userRegisterModel.Password != userRegisterModel.ConfirmPassword) { return(this.Redirect("/Users/Register")); } string hashedPass = this.HashPassword(userRegisterModel.Password); this.userService .CreateUser(userRegisterModel.Username, userRegisterModel.Email, hashedPass); return(this.Redirect("/Users/Login")); }
public IActionResult Register(UserRegisterBindingModel model) { if (this.User.IsAuthenticated) { return(this.RedirectToHome()); } if (!this.IsValidModel(model)) { this.ShowError(RegisterError); return(this.View()); } var user = this.userService.Create(model.Email, model.Password, (PositionType)model.Position); if (user) { return(this.RedirectToLogin()); } else { this.ShowError(EmailExistsError); return(this.View()); } }
public IActionResult Register(UserRegisterBindingModel user) { if (!this.IsValidModel(user)) { this.Model.Data["error"] = this.GetErrorMessageFromInfalidProp(user); return(this.View()); } if (!this.User.IsAuthenticated) { this.RedirectToHome(); } var userDb = new User { Username = user.Username, Email = user.Email, Password = PasswordUtilities.GetPasswordHash(user.Password) }; using (this.Context) { this.Context.Users.Add(userDb); this.Context.SaveChanges(); } this.SignIn(userDb.Username); return(this.RedirectToHome()); }
public IActionResult Register(UserRegisterBindingModel model) { if (model.Password != model.ConfirmPassword) { return(this.View()); } this.userService .RegisterUser(model.Username, model.Password, model.FullName, model.Email); var role = this.userService .GetUserRole(model.Username); var identityUser = new IdentityUser { Email = model.Email, Password = model.Password, Username = model.Username, Roles = new List <string> { role } }; this.SignIn(identityUser); return(this.RedirectToHome()); }
public async Task <IActionResult> Register(UserRegisterBindingModel model) => await this.Handle( async() => { var result = await this.identityService .Register(new UserInputModel() { Email = model.Email, Password = model.Password, Name = model.Name, PhoneNumber = model.PhoneNumber }); this.Response.Cookies.Append( AuthenticationCookieName, result.Token, new CookieOptions { HttpOnly = true, Secure = false, //for docker MaxAge = TimeSpan.FromDays(1) }); }, success : RedirectToAction("AddStudent", "Identity", new StudentBindingModel() { Email = model.Email, Name = model.Name, PhoneNumber = model.PhoneNumber }), failure : RedirectToAction(nameof(HomeController.Index), "Home"));
public IActionResult Register(UserRegisterBindingModel model) { if (this.User.IsAuthenticated) { return(RedirectToHome()); } if (!this.IsValidModel(model)) { this.Model.Data["error"] = this.GetErrorMessageFromInvalidProp(model); return(this.View()); } var userDb = new User { Email = model.Email, Name = model.Name, Password = PasswordUtilities.GetPasswordHash(model.Password) }; using (this.Context) { if (!this.Context.Users.Any()) { userDb.IsAdmin = true; } this.Context.Users.Add(userDb); this.Context.SaveChanges(); } this.SignIn(userDb.Name, userDb.Id); return(RedirectToHome()); }
public async Task <IActionResult> Register(UserRegisterBindingModel model) { if (this.ModelState.IsValid) { var user = new ApplicationUser { UserName = model.Username, PasswordHash = model.Password, Email = model.Email }; var result = await this.userManager.CreateAsync(user, model.Password); if (result.Succeeded) { await this.signManager.SignInAsync(user, false); return(this.RedirectToAction("Index", "Home")); } else { foreach (var error in result.Errors) { this.ModelState.AddModelError("", error.Description); } } } return(this.View()); }
public IActionResult Register(UserRegisterBindingModel model) { if (!this.IsValidModel(model)) { ShowError(model); return(this.View()); } if (this.User.IsAuthenticated) { return(this.RedirectToHome()); } var passwordHash = PasswordUtilities.GetPasswordHash(model.Password); using (this.Context) { var user = new User { Username = model.Username, Password = passwordHash, Email = model.Email, }; this.Context.Users.Add(user); this.Context.SaveChanges(); this.SignIn(user.Username, user.Id); } return(this.RedirectToHome()); }
public async Task <IActionResult> Register([FromBody] UserRegisterBindingModel model) { var user = await _userService.RegisterUser(new User() { Email = model.Email, UserName = model.Username, }, model.Password, UserService.UserRole); return(Ok(Mapper.Map <UserBindingModel>(user))); }
public void Register(UserRegisterBindingModel urbm) { bool isAdmin = !this.data.Users.GetAll().Any(); var user = Mapper.Map <UserRegisterBindingModel, User>(urbm); user.IsAdmin = isAdmin; this.data.Users.InsertOrUpdate(user); this.data.SaveChanges(); }
public void Register(UserRegisterBindingModel model) { var user = new User { Role = IdentityRole.User, Username = model.Username, Email = model.Email, Password = this.Hash(model.Password), }; this.context.Users.Add(user); this.context.SaveChanges(); }
public async Task <IActionResult> Register([FromBody] UserRegisterBindingModel user) { var token = await this.usersService.Register(user.Username, user.Email, user.Password); if (token is null) { return(Conflict(new { message = "Username or email is taken!" })); } return(Ok(new { token = token, name = user.Username, isAdmin = this.usersService.IsAdmin(token), message = "Registration successful!" })); }
public IActionResult Register(UserRegisterBindingModel model) { using (NoteAppContext context = new NoteAppContext()) { context.Users.Add(new User() { Username = model.Username, Password = $"SECRET{model.Password}" }); context.SaveChanges(); } return(this.View("Home", "Index")); }
internal void RegisterUser(UserRegisterBindingModel bindingModel) { User user = new User() { Username = bindingModel.Username, Email = bindingModel.Email, Password = bindingModel.Password, BirthDate = bindingModel.BirthDate }; this.context.Users.Add(user); this.context.SaveChanges(); }
public async Task <IActionResult> Register([FromBody] UserRegisterBindingModel model) { if (model == null || !this.ModelState.IsValid) { return(this.ValidationProblem(this.ModelState)); } var user = new User { FirstName = model.FirstName, LastName = model.LastName, Email = model.Email, UserName = model.Email }; var role = !await this.UserServices.AnyAsync() ? nameof(RoleType.Administrator) : nameof(RoleType.Regular); var creationResult = await this.UserServices.CreateAsync(user, model.Password); if (!creationResult.Succeeded) { return(this.BadRequest(this.ModelState)); } var addToRoleResult = await this.UserServices.AddToRoleAsync(user, role); if (!addToRoleResult.Succeeded) { return(this.BadRequest(addToRoleResult.GetAllErrors(this.ModelState))); } var response = this.UserServices.Authenticate(model.Email, model.Password, this.HttpContext, out user); if (response.Result == UserService.InvalidEmail) { this.ModelState.AddModelError(nameof(UserService.InvalidEmail), UserService.InvalidEmail); return(this.BadRequest(this.ModelState)); } if (response.Result == UserService.InvalidPassword) { this.ModelState.AddModelError(nameof(UserService.InvalidPassword), UserService.InvalidPassword); return(this.BadRequest(this.ModelState)); } response.Id = user.Id; response.FirstName = user.FirstName; response.LastName = user.LastName; response.Email = user.Email; response.AvatarPath = user.AvatarPath; return(this.Ok(response)); }
public async Task <ActionResult <string> > Register([FromBody] UserRegisterBindingModel model) { if (model == null || !ModelState.IsValid || model.PhoneNumber.Length < 5) { return(BadRequest(ModelState.Values.FirstOrDefault())); } var accountSid = options.Value.AccountSid; var authToken = options.Value.AuthToken; var fromNumber = options.Value.PhoneNumber; var verificationCode = await smsService.SendSms(accountSid, authToken, fromNumber, model.PhoneNumber); if (string.IsNullOrEmpty(verificationCode)) { return(BadRequest()); } if (await userManager.FindByNameAsync(model.PhoneNumber) == null) { var deviceId = await deviceService.AddDeviceAsync(model.Device); var user = new MrsMobileUser { Email = model.PhoneNumber, UserName = model.PhoneNumber, PhoneNumber = model.PhoneNumber, DeviceId = deviceId }; var result = await userManager.CreateAsync(user, model.PhoneNumber); if (!result.Succeeded) { return(BadRequest(result.Errors.FirstOrDefault())); } var addRole = await userManager.AddToRoleAsync(user, GlobalConstants.RoleUser); if (!addRole.Succeeded) { return(BadRequest(addRole.Errors.FirstOrDefault())); } } var token = await smsAuthanticationService.AddSmsTokenAsync(userManager.FindByNameAsync(model.PhoneNumber).Result.Id, verificationCode); if (!string.IsNullOrEmpty(token)) { return(token); } return(BadRequest()); }
public IActionResult Register(UserRegisterBindingModel model) { if (!ModelState.IsValid || model.Password != model.ConfirmPassword) return this.Redirect("/Users/Register"); User user = new User() { Username = model.Username, Email = model.Email, Password = HashPassword(model.Password) }; this.usersService.CreateUser(user); return this.Redirect("/Users/Login"); }
public void Register(HttpSession session, HttpResponse response, UserRegisterBindingModel userBindingModel) { if (this.securityService.IsAuthenticated(session)) { this.Redirect(response, "/home/index"); } if (!this.securityService.HasRegister(userBindingModel)) { this.Redirect(response, "/user/register"); return; } this.Redirect(response, "/user/login"); }
internal bool IsValidRegisterInput(UserRegisterBindingModel bindingModel) { if (string.IsNullOrEmpty(bindingModel.Username) || string.IsNullOrEmpty(bindingModel.Email) || string.IsNullOrEmpty(bindingModel.Password) || string.IsNullOrEmpty(bindingModel.ConfirmPassword)) { if (bindingModel.Password == bindingModel.ConfirmPassword) { return(false); } } return(true); }
public bool CreateUser(UserRegisterBindingModel userModel) { using (this.userRepository) { bool isUserWithSameUsernameExisting = this.userRepository.IsUserWithSameUsernameExisting(userModel.Username); if (isUserWithSameUsernameExisting) { return(false); } string passwordSalt = PasswordUtilities.GeneratePasswordSalt(); string passwordHash = PasswordUtilities.GeneratePasswordHash(userModel.Password, passwordSalt); UserCreateModel user = new UserCreateModel(userModel.Username, userModel.Name, passwordHash, passwordSalt); bool createUserResult = this.userRepository.CreateUser(user); return(createUserResult); } }
public IActionResult Register(UserRegisterBindingModel bindingModel, HttpResponse response, HttpSession currentSession) { if (this.loginManager.IsAuthenticated(currentSession)) { this.Redirect(response, "/users/feed"); return(null); } if (!this.userService.IsValidRegisterInput(bindingModel)) { this.Redirect(response, "/users/register"); return(null); } this.userService.RegisterUser(bindingModel); this.Redirect(response, "/users/login"); return(null); }
public IActionResult Register(HttpResponse response, UserRegisterBindingModel urbm, HttpSession session) { if (this.service.IsUserLoggedInOrRegistered(session)) { this.Redirect(response, "/home/index"); return null; } bool verified = this.service.VerifyRegistration(urbm); if (verified) { this.service.Register(urbm); this.Redirect(response, "/users/login"); return null; } this.Redirect(response, "/users/register"); return null; }
public ActionResult Register(UserRegisterBindingModel userModel) { if (!this.ModelState.IsValid) { return(this.View(userModel)); } bool registerResult = this.userManager.CreateUser(userModel); if (!registerResult) { this.TempData.AddErrorMessage(MessageConstants.ExistingUsernameError); return(this.View(userModel)); } this.TempData.AddSuccessMessage(MessageConstants.RegistrationSuccessful); return(RedirectToAction(nameof(UsersController.Login), WebConstants.UsersController)); }
public async Task <IActionResult> Register([FromBody] UserRegisterBindingModel collection) { if (string.IsNullOrWhiteSpace(collection?.DeviceId)) { return(BadRequest(GeneralMessage.DeviceIdNotFound)); } if (string.IsNullOrWhiteSpace(collection?.Phone) && string.IsNullOrWhiteSpace(collection?.Email)) { return(BadRequest(GeneralMessage.DefectiveEntry)); } try { var model = _mapper.Map <UserRegisterSchema>(collection); model.IP = IP; await _userService.RegisterAsync(model); switch (model.StatusCode) { case 1: return(Ok(data: model.Token)); case 2: return(Ok(status: HttpStatusCode.Accepted, message: "اعتبار سنجی دو مرحله ای فعال می باشد")); case -1: return(BadRequest(GeneralMessage.DefectiveEntry)); case -2: return(BadRequest("نام کاربری و یا کد فعال ساز شما اشتباه است")); case -3: return(BadRequest("تلفن و یا کد فعال ساز شما اشتباه است")); case -4: return(BadRequest("ایمیل و یا کد فعال ساز شما اشتباه است")); case -5: return(BadRequest(GeneralMessage.DeviceIdNotFound)); } } catch (Exception ex) { await _exceptionService.InsertAsync(ex, URL, IP); } return(InternalServerError()); }