public ActionResult Add([Bind("Username", "Password")] Account NewAccount) { var isValidUsername = AccountDomainService.isValidUsername(NewAccount.Username); //var isValidPassword = AccountDomainService.isValidPassword(NewAccount.Password); if (!isValidUsername) { return(HttpBadRequest("The username chosen is not valid.")); } Account newAccount = null; var existingAccount = _accountService.Get(NewAccount.Username); if (existingAccount != null) { return(HttpBadRequest("An account already exists with that username.")); } if (ModelState.IsValid) { newAccount = _accountService.Add(NewAccount); if (newAccount == null) { return(HttpBadRequest("Account creation failed.")); } var newUser = new User { Account = newAccount }; try { _userService.Add(newUser); } catch (Exception) { _accountService.Delete(newAccount); return(HttpBadRequest("Account creation failed. Unable to create user.")); } } return(new JsonResult(newAccount)); }
public async Task Add(UserAddInput input) { var user = input.Adapt <SysUser>(); user.FirstNameInitial = WordsHelper.GetFirstPinyin(user.Name.Substring(0, 1)); user.PasswordLevel = (PasswordLevel)H_Util.CheckPasswordLevel(user.Password); user.Password = H_EncryptProvider.HMACSHA256(user.Password, _appSettings.Key.Sha256Key); user.Enabled = true; var role = await _roleDomainService.Get(input.RoleId.Value); user.RoleId = role.Id; user.RoleName = role.Name; user.AuthNumbers = role.AuthNumbers; user.RoleLevel = role.Level; await _userDomainService.Add(user); }
public async Task <Unit> Handle(RegCommand command, CancellationToken cancellationToken) { var email = command.Email.Trim().ToLower(); var password = command.Password; var code = command.Code; var user = await _userDomainService.Get(p => p.Email == email && p.HasVerifiedEmail); if (user != null) { await _bus.RaiseEvent(new DomainNotification("邮箱已被注册,请更改!")); return(Unit.Value); } string key = string.Format(RedisKey.RegEmail, email);// $"regemail_{email}"; long ttl = await _redisDb.KeyTimeToLive(key); if (ttl < 0) { await _bus.RaiseEvent(new DomainNotification($"注册验证码已超时,请重试")); return(Unit.Value); } string emailCode = await _redisDb.StringGet <string>(key); if (string.Compare(emailCode, code, true) != 0) { await _bus.RaiseEvent(new DomainNotification($"注册验证码已失效,请重试")); return(Unit.Value); } string ip = _httpAccessor.HttpContext.GetUserIp(); user = new UserEntity { Email = email, LastDate = DateTime.Now, Password = password.ToMd5(), Status = UserStatusEnum.正常, RegDate = DateTime.Now, UserName = "", RegIp = ip, LastIp = ip, HasVerifiedEmail = true }; await _userDomainService.Add(user); var jwtAccount = new JwtAccount { UserId = user.Id, Email = user.Email }; await _httpAccessor.HttpContext.SignIn("user", jwtAccount); await _redisDb.KeyDelete(key); if (await Commit()) { await _bus.RaiseEvent(new SignUpEvent(user)).ConfigureAwait(false); } return(Unit.Value); }