public bool CheckUser(string username, string password, out FlowChatUserModel user) { user = new FlowChatUserModel(); int count = 0; var reader = sqliteHelper.ExecuteSqlReader("select * from User where Username = '******' and Password='******'"); while (reader.Read()) { user.Uid = int.Parse(reader["id"].ToString()); user.UserName = reader["Username"].ToString(); user.Password = reader["Password"].ToString(); count++; } reader.Close(); return(count > 0); }
public FlowChatSession(TcpClient tcpClient, FlowChatUserModel userData) { this.tcpClient = tcpClient; this.userData = userData; }
private void ParseLoginDataAndReturn(string jsonStr, NetworkStream stream) { FlowChatSendLoginDataJson sendLoginData = JsonConvert.DeserializeObject <FlowChatSendLoginDataJson>(jsonStr); if (sendLoginData.Type == "Login") { FlowChatUserModel loginUserData = sendLoginData.Data as FlowChatUserModel; Console.WriteLine("Client " + tcpClient.Client.RemoteEndPoint.ToString() + " loggin to the Server..."); if (userDAL.CheckUser(loginUserData.UserName, loginUserData.Password, out loginUserData)) { FlowChatReceiveLoginDataJson recvLoginData = new FlowChatReceiveLoginDataJson(); recvLoginData.Data = loginUserData; recvLoginData.Type = "Login"; recvLoginData.Status = 1; var sendBuffer = Encoding.UTF8.GetBytes(FlowChatConsts.NETWORK_RECV_DATA_LOGIN_REGISTER_PREFIX + "|" + recvLoginData.ToJSON()); stream.Write(sendBuffer, 0, sendBuffer.Length); loginedUserData = loginUserData; Console.WriteLine("Client " + tcpClient.Client.RemoteEndPoint.ToString() + " Login Success!"); } else { FlowChatReceiveLoginDataJson recvLoginData = new FlowChatReceiveLoginDataJson(); recvLoginData.Data = null; recvLoginData.Type = "Login"; recvLoginData.Status = 0; recvLoginData.Message = "Username or Password is incorrect"; tcpClient.Client.Send(Encoding.UTF8.GetBytes(FlowChatConsts.NETWORK_RECV_DATA_LOGIN_REGISTER_PREFIX + "|" + recvLoginData.ToJSON())); loginedUserData = null; Console.WriteLine("Client " + tcpClient.Client.RemoteEndPoint.ToString() + " Login Failed!"); } } else if (sendLoginData.Type == "Register") { FlowChatUserModel registerUserData = sendLoginData.Data as FlowChatUserModel; if (!userDAL.CheckUser(registerUserData.UserName, out registerUserData)) { userDAL.AddUser(registerUserData, out registerUserData); FlowChatReceiveLoginDataJson recvRegData = new FlowChatReceiveLoginDataJson(); recvRegData.Data = registerUserData; recvRegData.Type = "Register"; recvRegData.Status = 1; loginedUserData = registerUserData; tcpClient.Client.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(recvRegData))); } else { FlowChatReceiveLoginDataJson recvRegData = new FlowChatReceiveLoginDataJson(); recvRegData.Data = null; recvRegData.Type = "Register"; recvRegData.Status = 0; recvRegData.Message = "The User with the same username has already existed!"; loginedUserData = null; tcpClient.Client.Send(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(recvRegData))); } } }
private void Worker_DoWork(object sender, DoWorkEventArgs e) { object[] arr = new object[2]; try { FlowChatUserModel userModel = new FlowChatUserModel(); userModel.UserName = txtUsername.Text; userModel.Password = txtPassword.Text; FlowChatSendLoginDataJson sendLoginDataJson = new FlowChatSendLoginDataJson(); sendLoginDataJson.Type = "Login"; sendLoginDataJson.Data = userModel; string jsonStr = sendLoginDataJson.ToJSON(); jsonStr = FlowChatConsts.NETWORK_SEND_DATA_LOGIN_REGISTER_PREFIX + "|" + jsonStr; NetworkStream stream = tcpClient.GetStream(); var buffer = Encoding.UTF8.GetBytes(jsonStr); stream.Write(buffer, 0, buffer.Length); System.Threading.Thread.Sleep(1000); buffer = new byte[3096]; int len = stream.Read(buffer, 0, buffer.Length); byte[] newBuffer = new byte[len]; for (int i = 0; i < len; i++) { newBuffer[i] = buffer[i]; } var str = Encoding.UTF8.GetString(newBuffer); string[] tokens = str.Split('|'); switch (tokens[0]) { case FlowChatConsts.NETWORK_RECV_DATA_LOGIN_REGISTER_PREFIX: FlowChatReceiveLoginDataJson recvLoginData = JsonConvert.DeserializeObject <FlowChatReceiveLoginDataJson>(tokens[1]); if (recvLoginData.Status == 1) { var useData = recvLoginData.Data; session = new FlowChatSession(tcpClient, useData); arr[0] = "Success"; arr[1] = null; } else { arr[0] = "Failed"; arr[1] = "Login Failed!"; } break; } } catch (Exception ex) { arr[0] = "Failed"; arr[1] = ex.Message; } e.Result = arr; }
public void AddUser(FlowChatUserModel registerUser, out FlowChatUserModel user) { sqliteHelper.ExecuteSql("insert into User(Username, Password, Avatar) values('" + registerUser.UserName + "','" + registerUser.Password + "','" + registerUser.Avatar + "')"); CheckUser(registerUser.UserName, registerUser.Password, out user); }