using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.Drawing; using System.Text; using System.IO; public partial class login : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btnLogin_Click(object sender, EventArgs e) { if (txtUsername.Text.Trim() == "") lblError.Text = "Please input username"; else if (txtPassword.Text == "") lblError.Text = "Please input password"; else { string id = checkUser(txtUsername.Text.Trim(), txtPassword.Text); if (id != "") Response.Redirect("maplocation.aspx"); //?loginID=" + id); else lblError.Text = "Wrong username or password"; } } public string checkUser(string username, string password) { TextReader fileReader = new StreamReader(System.AppDomain.CurrentDomain.BaseDirectory.ToString() + @"\Data\user.csv"); try { string line = fileReader.ReadLine();//line1 line = fileReader.ReadLine(); while (line != "" || line != null) { string[] bits = line.Split(','); if (bits[0] == username && bits[1] == password) { fileReader.Close(); writeLog(bits[0]); return bits[2]; } else line = fileReader.ReadLine(); } } catch { fileReader.Close(); } return ""; } public void writeLog(string name) { //checkLog(); string path = System.AppDomain.CurrentDomain.BaseDirectory.ToString() + @"\Data\log.csv"; StreamWriter writeLog= new StreamWriter(path, true); try { DateTime time = DateTime.Now; HttpCookie cookie = new HttpCookie("Preferences"); cookie["Name"] = name; cookie.Expires = DateTime.Now.AddDays(1); Response.Cookies.Add(cookie); writeLog.WriteLine(name + ","+ time.Date + "," + time.TimeOfDay + ",login"); } catch {} writeLog.Close(); } public void checkLog() { string path = System.AppDomain.CurrentDomain.BaseDirectory.ToString() + @"\Data\log.csv"; StreamWriter writeLog = new StreamWriter(path, true); try { DateTime time = DateTime.Now; writeLog.WriteLine("," + time.Date + "," + time.TimeOfDay + ",logout"); } catch { } writeLog.Close(); } }