/// <summary> /// 用户注册方法 /// </summary> /// <param name="request"></param> /// <returns></returns> public UserAddResponse UserRegist(UserAddRequest request) { UserAddResponse response = new UserAddResponse(); if (string.IsNullOrEmpty(request.UserName)) { response.Status = false; response.Message = "手机号不能为空"; return(response); } if (string.IsNullOrEmpty(request.Pwd)) { response.Status = false; response.Message = "密码不能为空"; return(response); } //判断手机号是否已注册 var isExist = userDal.IsExistPhone(request.UserName); if (isExist) { response.Status = false; response.Message = "手机号已存在"; return(response); } //对密码进行加密 //1 生成一个盐 为什么要生成一个盐? var salt = Generate.GenerateSalt(); //2 加密 var password = MD5Encrypt.MD5Encrypt32(request.Pwd + salt); UserInfo user = new UserInfo() { UserName = request.UserName, PhoneNumder = request.UserName,//13621807335 NickName = request.UserName.ToNickName(), UserPass = password, Salt = salt }; var res = userDal.UserRegist(user); if (res > 0) { response.IsRegistSuccess = true; } else { response.Status = false; response.Message = "注册失败"; } return(response); }
public IActionResult AddNewUser([FromBody] UserAddRequest addRequest) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } UserAddResponse response = _usersControllerUtilities.AddNewUser(addRequest); return(Ok(response)); }
/// <summary> /// Add the specified user. /// </summary> /// <returns>The add.</returns> /// <param name="user">User.</param> public async Task <UserAddResponse> Add(User user) { // Add response UserAddResponse userAddResponse = new UserAddResponse(); // Check if user email or cpf exists User[] checkUser = await(from u in db.Users where (u.Email == user.Email || u.Cpf == user.Cpf) where u.ID != user.ID select u).Take(1).ToArrayAsync(); if (checkUser.Any()) { if (checkUser.FirstOrDefault().Email == user.Email) { userAddResponse.ErrorEmail = true; } if (checkUser.FirstOrDefault().Cpf == user.Cpf) { userAddResponse.ErrorCpf = true; } return(userAddResponse); } user.Password = new HashPassword().Generate(user.Password); user.CreatedAt = DateTime.Now; user.ID = 0; db.Users.Add(user); // Save Log int userId = int.Parse(Thread.CurrentPrincipal.Identity.Name); Log log = new Log { UserID = userId, Action = "user.add" }; await new LogService().Save(log); Claim[] claims = { new Claim(ClaimTypes.Name, user.FirstName), new Claim(ClaimTypes.Email, user.Email), new Claim(ClaimTypes.Role, user.Role) }; userAddResponse.TokenType = "Bearer"; userAddResponse.AccessToken = _tokenManager.Generate(claims); return(userAddResponse); }
public UserAddResponse UserRegist(UserAddRequest request) { UserAddResponse response = new UserAddResponse(); if (string.IsNullOrEmpty(request.UserName)) { response.Status = false; response.Message = "用户名不能为空"; return(response); } if (string.IsNullOrEmpty(request.Pwd)) { response.Status = false; response.Message = "密码不能为空"; return(response); } //是否已经注册 var isExist = dal.IsExistPhone(request.UserName); if (isExist) { response.Status = false; response.Message = "用户名已经存在"; return(response); } //对密码进行加密 //1.先生成一个盐 var salt = Generate.GenerateSalt(); //2.加密 var password = MD5Encrypt.MD5Encrypt32(request.Pwd); UserInfo userInfo = new UserInfo() { UserName = request.UserName, Phone = request.Phone, PassWord = password, Salt = salt }; var res = dal.UserRegist(userInfo); if (res > 0) { response.RegistSuccess = true; response.Message = "注册成功"; } else { response.RegistSuccess = false; response.Message = "注册失败"; } return(response); }
public async Task <object> Add([FromBody] User user) { if (!ModelState.IsValid) { return(BadRequest(ModelState.Values.SelectMany(start => start.Errors).Select(error => error.ErrorMessage).Take(1).ElementAt(0))); } UserAddResponse addUser = await _userService.Add(user); if (addUser.ErrorEmail) { return(BadRequest("error.user.email-exists")); } if (addUser.ErrorCpf) { return(BadRequest("error.user.cpf-exists")); } return(Ok(addUser)); }
public IActionResult Post([FromBody] UserAddRequest addRequest) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } UserAddResponse response = new UserAddResponse { ErrorCode = (int)UserResponseCode.Success, Name = addRequest.Name, FailReason = "", UserId = "" }; if (_userDatabase.LoadedItems.Any(x => x.Name == addRequest.Name)) { response.ErrorCode = (int)UserResponseCode.DuplicateName; response.FailReason = "Cannot Add as User name already exists"; return(Ok(response)); } if (_userDatabase.LoadedItems.Any(x => x.Email == addRequest.Email)) { response.ErrorCode = (int)UserResponseCode.DuplicateEmail; response.FailReason = "Cannot Add as User e-mail already exists"; return(Ok(response)); } User newUser = new User(addRequest.Name, addRequest.Password, addRequest.Email, addRequest.Description); _userDatabase.AddNewItemToDatabase(newUser); response.UserId = newUser.Id.ToString(); return(Ok(response)); }
/// <summary> /// Adds a new user login. /// </summary> /// <param name="addRequest">The new user login to try to add.</param> /// <returns>The action result.</returns> public UserAddResponse AddNewUser(UserAddRequest addRequest) { UserAddResponse response = new UserAddResponse { ErrorCode = (int)UserResponseCode.Success, Name = addRequest.Name, FailReason = "", UserId = "" }; if (_userDatabase.LoadedItems.Any(x => x.Name == addRequest.Name)) { response.ErrorCode = (int)UserResponseCode.DuplicateName; response.FailReason = "Cannot Add as User name already exists"; return(response); } if (_userDatabase.LoadedItems.Any(x => x.Email == addRequest.Email)) { response.ErrorCode = (int)UserResponseCode.DuplicateEmail; response.FailReason = "Cannot Add as User e-mail already exists"; return(response); } User newUser = new User(addRequest.Name, addRequest.Password, addRequest.Email, addRequest.Description); _userDatabase.AddNewItemToDatabase(newUser); // Send the authorization e-mail. SendAuthorizationEmail(newUser); // return the unverified e-mail to the user display response.UserId = newUser.Id.ToString(); return(response); }
/// <summary> /// 用户添加 /// </summary> /// <param name="request"></param> /// <returns></returns> public UserAddResponse UserAdd(UserAddRequest request) { UserAddResponse response = new UserAddResponse(); //非空判断 if (string.IsNullOrEmpty(request.User.UserName)) { response.Status = false; response.Message = "用户名为空"; return(response); } if (string.IsNullOrEmpty(request.User.UserPassword)) { response.Status = false; response.Message = "密码为空"; return(response); } if (string.IsNullOrEmpty(request.User.Email)) { response.Status = false; response.Message = "邮箱为空"; return(response); } if (request.User.AddressId <= 0) { response.Status = false; response.Message = "请选择地址"; return(response); } if (request.User.RoleId <= 0) { response.Status = false; response.Message = "请选择角色"; return(response); } if (request.User.CreatorId <= 0) { response.Status = false; response.Message = "系统繁忙,creatorid<=0"; return(response); } //判断用户名是否已被注册 int uid = UserDal.Instance.UserNameExist(request.User.UserName); if (uid > 0) { response.Status = false; response.Message = "用户名已存在"; return(response); } //开始获取盐 var salt = Generate.GenerateSalt(); //获取md5加密密码 var pwd = MD5Encrypt.MD5Encrypt32(request.User.UserPassword + salt); request.User.UserPassword = pwd; request.User.Salt = salt; var res = UserDal.Instance.UserAdd(request.User); if (res < 0) { response.Status = false; response.Message = "添加失败"; } else { response.Message = "添加成功"; } return(response); }
/// <summary> /// 添加用户 /// </summary> public void AddUser(UserAddRequest userAddRequest) { UserAddResponse response = new UserAddResponse(); ApiRequestHelper.Post <UserAddRequest, UserAddResponse>(userAddRequest); }