Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Diagnostics;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- // To detect internet connection detection
- using System.Net;
- using System.Net.Sockets;
- using System.Runtime.InteropServices;
- // For periodically run specific function
- // add a timer to form
- // for SQLite
- using System.Data.SQLite;
- using System.IO;
- namespace kospen
- {
- public partial class Login : Form
- {
- // API for internet connection detection
- [DllImport("wininet.dll")]
- private extern static bool InternetGetConnectedState(out int Description, int ReservedValue);
- public Login()
- {
- InitializeComponent();
- }
- // function to detect internet connection
- public static bool InternetExist()
- {
- try
- {
- int ConnDesc;
- return InternetGetConnectedState(out ConnDesc, 0);
- // if connection detected, it will return true
- }
- catch
- {
- return false;
- }
- }
- private void linkLabelRegister_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
- {
- //ProcessStartInfo sInfo = new ProcessStartInfo("http://offline.moves.moh.gov.my/register");
- ProcessStartInfo sInfo = new ProcessStartInfo("http://offline.moves.alqadam.net/register");
- Process.Start(sInfo);
- }
- private void Login_Load(object sender, EventArgs e)
- {
- // we put timer on form..so it will always reload this page at 100ms
- // put some function to detect internet
- Boolean internetDetected = InternetExist();
- if (internetDetected)
- {
- toolStripStatusLabelOnlineStatus.Text = "ONLINE";
- toolStripStatusLabelOnlineStatus.ForeColor = Color.Green;
- }
- else
- {
- toolStripStatusLabelOnlineStatus.Text = "OFFLINE";
- toolStripStatusLabelOnlineStatus.ForeColor = Color.Red;
- }
- }
- private void buttonLogin_Click(object sender, EventArgs e)
- {
- String username = textBoxUsername.Text;
- String password = textBoxPassword.Text;
- String userNamaDb = "";
- String userIdDb = "";
- String usernameDb = "";
- String passwordDb = "";
- Boolean loginPass = false;
- Boolean internetDetected = InternetExist();
- int rowCount;
- // Boolean internet_connection = true;
- Boolean internet_connection = internetDetected;
- //check if online
- if (internet_connection == true)
- {
- //authenticate with webserver
- try
- {
- string user = username;
- string pass = password;
- ASCIIEncoding encoding = new ASCIIEncoding();
- string postData = "email=" + user + "&password=" + pass;
- byte[] data = encoding.GetBytes(postData);
- WebRequest request = WebRequest.Create("http://www.moves.moh.gov.my/kospenoffline/result");
- //WebRequest request = WebRequest.Create("http://www.moves.moh.gov.my/training/kospenoffline/result");
- //WebRequest request = WebRequest.Create("http://offline.kospen.zulfatech.com/result");
- //WebRequest request = WebRequest.Create("http://offline.moves.alqadam.net/result");
- //WebRequest request = WebRequest.Create("http://domb.app/result");
- request.Method = "POST";
- request.ContentType = "application/x-www-form-urlencoded";
- request.ContentLength = data.Length;
- Stream stream = request.GetRequestStream();
- stream.Write(data, 0, data.Length);
- stream.Close();
- WebResponse response = request.GetResponse();
- stream = response.GetResponseStream();
- StreamReader sr = new StreamReader(stream);
- //split response into array
- String statusAuth = sr.ReadToEnd().ToString();
- string[] tokens = statusAuth.Split(',');
- if (tokens[0] == "true")
- {
- loginPass = true;
- //Get array response
- String id = tokens[1];
- String nama = tokens[2];
- String no_kp = tokens[3];
- String no_tel = tokens[4];
- String email = tokens[5];
- String uname = tokens[6];
- String passw = tokens[7];
- String permissions = tokens[8];
- String activated = tokens[9];
- String activation_code = tokens[10];
- String activated_at = tokens[11];
- String last_login = tokens[12];
- String persist_code = tokens[13];
- String reset_password_code = tokens[14];
- String created_at = tokens[15];
- String updated_at = tokens[16];
- //patch by firdaus 4/2/2016
- String ud_id = tokens[17];
- String ud_neg_id = tokens[18];
- String ud_dae_id = tokens[19];
- String ud_use_id = tokens[20];
- String ud_lokaliti_id = tokens[21];
- String ud_created_at = tokens[22];
- String ud_updated_at = tokens[23];
- // insert into database
- SQLiteConnection conn = new SQLiteConnection("data source=kospen.sqlite");
- conn.Open();
- SQLiteCommand cmd = new SQLiteCommand(conn);
- SQLiteCommand cmdLatestId = new SQLiteCommand();
- SQLiteCommand cmdUsers = new SQLiteCommand(conn);
- // convert plain password into base64
- var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(passw);
- String passwEnc = System.Convert.ToBase64String(plainTextBytes);
- cmd.CommandText = "SELECT COUNT(*) FROM auth where id='" + id + "'";
- cmd.CommandType = CommandType.Text;
- rowCount = Convert.ToInt32(cmd.ExecuteScalar());
- cmdUsers.CommandText = "SELECT COUNT(*) FROM users where id='" + id + "'";
- cmdUsers.CommandType = CommandType.Text;
- int rowCountUsers = Convert.ToInt32(cmdUsers.ExecuteScalar());
- if (rowCount.ToString() == "0")
- {
- cmd.CommandText = "INSERT INTO auth (id,nama,no_kp,no_tel,email,username,password,permissions,activated,activation_code,activated_at,last_login,persist_code,reset_password_code,created_at,updated_at) " +
- "VALUES ('" + id + "','" +
- nama + "','" +
- no_kp + "','" +
- no_tel + "','" +
- email + "','" +
- uname + "','" +
- passwEnc + "','" +
- permissions + "','" +
- activated + "','" +
- activation_code + "','" +
- activated_at + "','" +
- last_login + "','" +
- persist_code + "','" +
- reset_password_code + "','" +
- created_at + "','" +
- updated_at + "')";
- var status = cmd.ExecuteNonQuery();
- }
- else
- {
- cmd.CommandText = "UPDATE auth SET nama = '" + nama +
- "',no_kp = '" + no_kp +
- "',no_tel = '" + no_tel +
- "',email = '" + email +
- "',username = '" + uname +
- "',permissions = '" + permissions +
- "',activated = '" + activated +
- "',activation_code = '" + activation_code +
- "',activated_at = '" + activated_at +
- "',last_login = '" + last_login +
- "',persist_code = '" + persist_code +
- "',reset_password_code = '" + reset_password_code +
- "',created_at = '" + created_at +
- "',updated_at = '" + updated_at + "' where id ='" + id + "'";
- cmd.ExecuteNonQuery();
- }
- if (rowCountUsers.ToString() == "0")
- {
- cmd.CommandText = "INSERT INTO users (id,nama,no_kp,no_tel,email,username,password,permissions,activated,activation_code,activated_at,last_login,persist_code,reset_password_code,created_at,updated_at) " +
- "VALUES ('" + id + "','" +
- nama + "','" +
- no_kp + "','" +
- no_tel + "','" +
- email + "','" +
- uname + "','" +
- passwEnc + "','" +
- permissions + "','" +
- activated + "','" +
- activation_code + "','" +
- activated_at + "','" +
- last_login + "','" +
- persist_code + "','" +
- reset_password_code + "','" +
- created_at + "','" +
- updated_at + "')";
- cmd.ExecuteNonQuery();
- cmd.CommandText = "INSERT INTO users_details (id,negeri_id,daerah_id,users_id,lokaliti_id,created_at,updated_at) " +
- "VALUES ('" + ud_id + "','" +
- ud_neg_id + "','" +
- ud_dae_id + "','" +
- ud_use_id + "','" +
- ud_lokaliti_id + "','" +
- ud_created_at + "','" +
- ud_updated_at + "')";
- cmd.ExecuteNonQuery();
- }
- else
- {
- cmd.CommandText = "UPDATE users SET nama = '" + nama +
- "',no_kp = '" + no_kp +
- "',no_tel = '" + no_tel +
- "',email = '" + email +
- "',username = '" + uname +
- "',permissions = '" + permissions +
- "',activated = '" + activated +
- "',activation_code = '" + activation_code +
- "',activated_at = '" + activated_at +
- "',last_login = '" + last_login +
- "',persist_code = '" + persist_code +
- "',reset_password_code = '" + reset_password_code +
- "',created_at = '" + created_at +
- "',updated_at = '" + updated_at + "' where id ='" + id + "'";
- cmd.ExecuteNonQuery();
- cmd.CommandText = "UPDATE users_details SET id = '" + ud_id +
- "',negeri_id = '" + ud_neg_id +
- "',daerah_id = '" + ud_dae_id +
- "',users_id = '" + ud_use_id +
- "',lokaliti_id = '" + ud_lokaliti_id +
- "',created_at = '" + ud_created_at +
- "',updated_at = '" + ud_updated_at + "' where users_id ='" + ud_use_id + "'";
- cmd.ExecuteNonQuery();
- }
- }
- else
- {
- loginPass = false;
- }
- sr.Close();
- stream.Close();
- }
- catch (Exception ex)
- {
- MessageBox.Show("Error : " + ex.Message);
- }
- }
- else
- {
- // Execute here when offline
- // Establish connection with local sqlite
- SQLiteConnection conn = new SQLiteConnection("data source=kospen.sqlite");
- conn.Open();
- var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(password);
- String passwEnc = System.Convert.ToBase64String(plainTextBytes);
- // count record
- SQLiteCommand cmd2 = new SQLiteCommand(conn);
- cmd2.CommandText = "SELECT COUNT(*) FROM auth where email='"+username+ "' and password = '"+ passwEnc + "'";
- cmd2.CommandType = CommandType.Text;
- rowCount = Convert.ToInt32(cmd2.ExecuteScalar());
- //if record count equal to 1 means user is valid
- if(rowCount.ToString() == "1")
- {
- loginPass = true;
- }
- else
- {
- loginPass = false;
- }
- }
- // Establish connection
- SQLiteConnection con = new SQLiteConnection("data source=kospen.sqlite");
- con.Open();
- // SQL command start here
- SQLiteCommand cmd1 = new SQLiteCommand(con);
- cmd1.CommandText = "SELECT * FROM users WHERE email='"+username+"'";
- SQLiteDataReader reader = cmd1.ExecuteReader();
- while (reader.Read())
- {
- usernameDb = reader["email"].ToString();
- passwordDb = reader["no_kp"].ToString();
- userNamaDb = reader["nama"].ToString();
- userIdDb = reader["id"].ToString();
- }
- // Check authencation pass or not using local storage
- if (loginPass)
- {
- this.Hide();
- var fListPatients = new FormListPatients( userIdDb, userNamaDb);
- // if new form is close, we want the original form to be closed as well. Else, mem leak nanti.
- fListPatients.Closed += (s, args) => this.Close();
- fListPatients.Show();
- }
- // count record
- // SQLiteCommand cmdCount = new SQLiteCommand(con);
- // cmdCount.CommandText = "SELECT COUNT(*) FROM users";
- // cmdCount.CommandType = CommandType.Text;
- // rowCount = Convert.ToInt32(cmdCount.ExecuteScalar());
- // MessageBox.Show(rowCount.ToString());
- // SQL command end here
- con.Close();
- // If not, the user may not exist in our local storage/db, but
- // already registered in the server. So, check internet connection
- // and sync database. Ask user either they want to sync db with server or not
- if (loginPass == false)
- {
- DialogResult dbSync = MessageBox.Show("Kombinasi katalaluan yang salah. " +
- "Anda perlukan akses internet sekiranya ini adalah kali pertama anda menggunakan akaun anda di aplikasi ini");
- if (dbSync == DialogResult.Yes)
- {
- // User want to sync database with the server. If internet exist, proceed with update, else notify user to connect to the internet
- if (InternetExist())
- {
- // perform data sync
- }
- else
- {
- MessageBox.Show("No internet connection. Please connect to the Internet and try again once the connection status become ONLINE");
- }
- }
- }
- // Then perform authencation using local one.
- }
- private void pictureBox1_Click(object sender, EventArgs e)
- {
- // FormInit systemInitializer = new FormInit();
- // systemInitializer.Show();
- }
- private void textBoxPassword_TextChanged(object sender, EventArgs e)
- {
- }
- private void textBoxUsername_TextChanged(object sender, EventArgs e)
- {
- }
- private void label2_Click(object sender, EventArgs e)
- {
- }
- private void label1_Click(object sender, EventArgs e)
- {
- }
- }
- }
Add Comment
Please, Sign In to add comment