public static void CreateUser(User request, IDacUser dacUser) { if (dacUser.Single(u => u.Username.Equals(request.Username)) != null) { throw new Exception("username already used"); } if (dacUser.Single(u => u.Email.Equals(request.Email)) != null) { throw new Exception("email already used"); } var user = new Data.Entity.User { Id = request.Id, Username = request.Username, Password = Crypt.ToSHA256(request.Password), Name = request.Name, Email = request.Email, Gender = request.Gender, Phone = request.Phone, Role = request.Role }; dacUser.Insert(user); dacUser.Commit(); }
public ActionResult <CollectionResponse <User> > GetSingle(string id) { var response = new CollectionResponse <User>(); try { response.Collections = TranslateToModel(new[] { _dacUser.Single(id) }); response.Status.SetSuccess(); } catch (Exception e) { response.Status.SetError(e); } return(response); }
public ActionResult <LoginResponse> Login([FromBody] LoginRequest request) { var response = new LoginResponse(); try { var passwordHashed = Crypt.ToSHA256(request.Password); var user = _dacUser.Single(u => u.Username.Equals(request.Username) && u.Password.Equals(passwordHashed)); if (user == null) { throw new Exception("username or password doesn't match"); } var key = Guid.NewGuid().ToString("N"); SetSessionIdCache(key, JsonConvert.SerializeObject(user)); response.SessionId = key; response.User = new User { Id = user.Id, Email = user.Email, Gender = user.Gender, Name = user.Name, Role = user.Role, Username = user.Username, Phone = user.Phone }; response.Status.SetSuccess(); } catch (Exception e) { response.Status.SetError(e.Message); } return(response); }