using Finisar.SQLite; using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.NetworkInformation; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; using System.Windows; namespace Live_Scoring_V1 { class Loginn { //LOGIN PROCESS SqlDatabase SQL = new SqlDatabase(); public int LoginProcess(string UserName, string Password) { int loggedin = CheckOnline( UserName, Password); //MessageBox.Show("Hello, world!"); return loggedin; } public bool IsLoggedIN() { bool loggedin = false; //MessageBox.Show("Hello, world!"); return loggedin; } // LOGIN LOCALLY public bool LoggedOut() { bool loggedin = false; try { string sql = "UPDATE Users SET " + "LoggedIn = 0 " + " WHERE LoggedIn = 1; "; bool update = SQL.Execute(sql); if (update) { loggedin = true; } } catch(Exception Ex) { MessageBox.Show(Ex.ToString()); } return loggedin; } // LOGIN ONLINE private int CheckOnline(string UserName, string Password) { int loggedin = 0; //Login //string URI = "https://footprintapp.net/api/login/login"; string URI = "https://qa.footprintapp.net/api/login/login"; //QA string myParameters = "UserName=" + UserName + "&Password=" + Password; using (WebClient wc = new WebClient()) { wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded"; string HtmlResult = wc.UploadString(URI, myParameters); var JsonObjects = Newtonsoft.Json.JsonConvert.DeserializeObject(HtmlResult); int UserID = Int32.Parse(JsonObjects.UserID.ToString()); int UnionID = Int32.Parse(JsonObjects.UnionID.ToString()); string FirstName = JsonObjects.FirstName.ToString(); string LastName = JsonObjects.LastName.ToString(); string FullName = JsonObjects.FullName.ToString(); string Email = JsonObjects.Email.ToString(); if (UserID >0) { var sql = "insert into Users (UserID, UnionID,FirstName,LastName,FullName,UserName," + "Password,LoggedIn)" + " values ('" + UserID + "', '" + UnionID + "','" + FirstName + "','" + LastName + "'," + "'" + FullName + "','" + Email + "','" + Password + "','1')"; bool Insert = SQL.Execute(sql); if (Insert == true) { loggedin = UserID; } else { sql = "UPDATE Users SET LoggedIn = 1," + " UserID = '" + UserID + "'," + " FirstName = '" + FirstName + "'," + " LastName = '" + LastName + "'," + " FullName = '" + FullName + "'," + " UserName = '" + Email + "'," + " Password = '" + Password + "' " + " WHERE UserID = '" + UserID + "'; "; bool update = SQL.Execute(sql); if (update) { loggedin = UserID; } } } } return loggedin; } private static String GetMD5Hash(String TextToHash) { //Check wether data was passed if ((TextToHash == null) || (TextToHash.Length == 0)) { return String.Empty; } //Calculate MD5 hash. This requires that the string is splitted into a byte[]. MD5 md5 = new MD5CryptoServiceProvider(); byte[] textToHash = Encoding.Default.GetBytes(TextToHash); byte[] result = md5.ComputeHash(textToHash); //Convert result back to string. return System.BitConverter.ToString(result); } public static string MD5Hash(string input) { StringBuilder hash = new StringBuilder(); MD5CryptoServiceProvider md5provider = new MD5CryptoServiceProvider(); byte[] bytes = md5provider.ComputeHash(new UTF8Encoding().GetBytes(input)); for (int i = 0; i < bytes.Length; i++) { hash.Append(bytes[i].ToString("x2")); } return hash.ToString(); } public int IsLoggedIN_AlReady() { int Logged = 0; /* * Ping ping = new Ping(); PingReply pingReply = ping.Send("197.242.158.8"); if (pingReply.Status == IPStatus.Success) { //Machine is alive Logged = 0; }*/ SQLiteConnection sqlite_conn; SQLiteCommand sqlite_cmd; SQLiteDataReader sqlite_datareader; string dbPath = (Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)) + "\\Sideline Manager\\"; try { sqlite_conn = new SQLiteConnection("Data Source="+ dbPath +"Footprint.db;Version=3;"); // open the connection: sqlite_conn.Open(); // create a new SQL command: sqlite_cmd = sqlite_conn.CreateCommand(); sqlite_cmd.CommandText = "SELECT * FROM Users Where Loggedin =1 "; // Now the SQLiteCommand object can give us a DataReader-Object: try { using (sqlite_datareader = sqlite_cmd.ExecuteReader()) { // The SQLiteDataReader allows us to run through the result lines: while (sqlite_datareader.Read()) // Read() returns true if there is still a result line to read { // Print out the content of the text field: //System.Console.WriteLine(sqlite_datareader["FirstName"]); Logged =Int32.Parse(sqlite_datareader["UserID"].ToString()); } } } catch { } // We are ready, now lets cleanup and close our connection: sqlite_conn.Close(); } catch (Exception e) { SqlDatabase DB = new SqlDatabase(); DB.CreateDataBase(); } return Logged; } } }