public Object Register(String username, String password, bool remember) { try { PhotoAppContext db = new PhotoAppContext(); SHA256 sha = SHA256Managed.Create(); byte[] password_digest = sha.ComputeHash(Utils.GetBytes(password + "584967")); var list = db.users.ToList(); User user = list.Where(x => x.username == username).FirstOrDefault(); if (user != null) { return JObject.Parse("{ success: false, error: 'Username is already taken' }"); } User u = new User { username = username, password = password_digest }; db.users.Add(u); db.SaveChanges(); HttpCookie cookie = new HttpCookie("PhotoApp"); cookie.Value = Utils.GetString(password_digest); Response.Cookies.Add(cookie); Session["username"] = username; Session["password"] = password_digest; return JObject.Parse("{ success: true, username: '******' }"); } catch (Exception e) { return JObject.Parse("{ success: false, error: '" + e.Message + "' }"); } }
public Object Login(String username, String password, bool remember) { try { PhotoAppContext db = new PhotoAppContext(); SHA256 sha = SHA256Managed.Create(); byte[] password_digest = sha.ComputeHash(Utils.GetBytes(password + "584967")); User u = db.users.Where(x => x.username == username).FirstOrDefault(); if (u == null) { return JObject.Parse("{ success: false, error: 'Username/password is invalid' }"); } else { if (Utils.ArrayCompare(u.password, password_digest)) { HttpCookie cookie = new HttpCookie("PhotoApp"); cookie.Value = Utils.GetHex(password_digest); cookie.Expires = DateTime.Now.AddHours(1); Response.Cookies.Add(cookie); Session["username"] = username; Session["password"] = password_digest; return JObject.Parse("{ success: true, username: '******' }"); } else { return JObject.Parse("{ success: false, error: 'Username/password is invalid' }"); } } } catch (Exception e) { return JObject.Parse("{ success: false, error: '" + e.Message + "' }"); } }