public ActionResult CreateUser(CreateUserBindingModel model) { if (ModelState.IsValid) { CreateUserResultModel newUser = new CreateUserResultModel { Username = model.Username, Password = model.Password }; try { //var post = WebRequestManager.HttpPost("http://localhost:52680/user/createuser/",model); using (HttpClient client = new HttpClient()) { // string stringData = JsonConvert.SerializeObject(model); // var contentData = new StringContent // (stringData, System.Text.Encoding.UTF8, // "application/json"); // var json = new JavaScriptSerializer().Serialize(newUser); // var content = new StringContent(json.ToString(), Encoding.UTF8, "application/json"); // var res = client.PostAsJsonAsync("http://localhost:52680/user/createuser", content).Result; // client.BaseAddress = new Uri("http://localhost:52680/user/createuser"); var postTask = client.PostAsJsonAsync("http://localhost:52680/user/createuser/", newUser); postTask.Wait(); var result = postTask.Result; if (result.IsSuccessStatusCode) { ViewBag.Title = "Success. Log in."; return(RedirectToAction("UserLogin", "User")); } // WebRequestManager.HttpPost("http://localhost:52680/user/createuser/", newUser); } } catch (Exception ex) { var err = ex; } } return(View("~/Views/Home/Index.cshtml")); }
public CreateUserResultModel CreateUser([FromBody] CreateUserBindingModel userModel) { var newUser = new CreateUserResultModel(); if (ModelState.IsValid) { newUser.Username = userModel.Username; newUser.Password = userModel.Password; try { using (SqlConnection connection = new SqlConnection(ConnectionStrings.connectionString)) { SqlCommand command = new SqlCommand(UserQueries.CreateUser, connection); command.Parameters.AddWithValue("@inUserame", newUser.Username); command.Parameters.AddWithValue("@inPassword", newUser.Password); connection.Open(); SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { string pass = (string)(reader["Password"] == DBNull.Value ? "" : reader["Password"].ToString()); string user = (string)(reader["User"] == DBNull.Value ? "" : reader["User"].ToString()); int code = (int)(reader["Code"] == DBNull.Value ? -1 : int.Parse(reader["Code"].ToString())); string message = (string)(reader["Msg"] == DBNull.Value ? "" : (reader["Msg"].ToString())); newUser = new CreateUserResultModel() { Code = code, Message = message, Username = user, Password = pass }; } reader.Close(); } } catch (Exception ex) { var error = ex.Message; } } return(newUser); }
public CreateUserResultModel CreateUser(UserModel userModel) { var result = new CreateUserResultModel(); UserRepository repo = new UserRepository(); var salt = repo.GenerateSalt(); var hash = repo.HashAndSaltPwd(userModel.Password, salt); var userCreationResult = repo.CreateUser(userModel, hash, salt); if (userCreationResult.IsSuccess) { result.IsSuccess = userCreationResult.IsSuccess; result.CreatedUserName = userCreationResult.UserCreated.Username; } else { result.IsSuccess = false; result.ErrorMessage = userCreationResult.ErrorMessage; } return(result); }