public ReceptmodelsAddEditView GetFoodForEditView(string psql, int pid_user) { postgres m = new postgres(); DataTable dt = new DataTable(); dt = m.SqlQuery(psql, postgres.list = new List <NpgsqlParameter>() { new NpgsqlParameter("@id_user", pid_user) }); ReceptmodelsAddEditView r = new ReceptmodelsAddEditView(); foreach (DataRow dr in dt.Rows) { r.Id = (int)dr["id_recept"]; r.Name = dr["name"].ToString(); r.Description = (string)dr["description"]; r.Url_pic = (string)dr["url_pic"]; r.Url_recept = (string)dr["url_recept"]; r.Ica_id = (int)dr["ica_id"]; r.CookingtimeId = (int)dr["cookingtime_id"]; r.TypeOfFoodId = (int)dr["type_of_food_id"]; r.Occasions = (int)dr["occasion_id"]; r.Rating = (double)dr["average_rating"]; } return(r); }
public List <Receptmodels> GetFood(string psql, int pid_user) { postgres m = new postgres(); DataTable dt = new DataTable(); List <Receptmodels> mt = new List <Receptmodels>(); dt = m.SqlQuery(psql, postgres.list = new List <NpgsqlParameter>() { new NpgsqlParameter("@id_user", pid_user) }); foreach (DataRow dr in dt.Rows) { Receptmodels r = new Receptmodels(); r.Id = Convert.ToInt16(dr["id_recept"].ToString()); r.Name = dr["name"].ToString(); r.Description = (string)dr["description"]; r.Url_pic = (string)dr["url_pic"]; r.Url_recept = (string)dr["url_recept"]; mt.Add(r); } return(mt); }
public Tuple <int, bool, string> Resetpassword(string validate) { postgres sql = new postgres(); DataTable dt = new DataTable(); dt = sql.SqlQuery("SELECT login.login_id, login.reset_hash, login.reset_time, users.username FROM login LEFT JOIN users ON users.login_id = login.login_id WHERE reset_hash = @hash;", postgres.list = new List <NpgsqlParameter>() { new NpgsqlParameter("@hash", validate) }); string hash = ""; DateTime date = DateTime.Now; int login_id = 0; string username = ""; foreach (DataRow dr in dt.Rows) { hash = dr["reset_hash"].ToString(); username = dr["username"].ToString(); date = (DateTime)dr["reset_time"]; login_id = (int)dr["login_id"]; } if (!string.IsNullOrEmpty(hash)) { if (validate == hash && date >= DateTime.UtcNow) { return(Tuple.Create(login_id, true, username)); } } return(Tuple.Create(login_id, false, username)); }
public Tuple <int, bool, string> AuthenticationUser(string ppassword, string userNameOrEmail) { byte[] salt = null, key = null; int user_id = 0; postgres m = new postgres(); string role = ""; bool active; string sql; userNameOrEmail = userNameOrEmail.ToLower(); sql = "select user_id, name, acc_active , login.salt , login.hash from users LEFT JOIN roles ON roles_id = id_roles LEFT JOIN login ON users.login_id = login.login_id where email =@par1 or users.username =@par1 "; var dt = m.SqlQuery(sql, postgres.list = new List <NpgsqlParameter>() { new NpgsqlParameter("@par1", userNameOrEmail), }); try { foreach (DataRow dr in dt.Rows) { salt = (byte[])dr["salt"]; key = (byte[])dr["hash"]; user_id = (int)dr["user_id"]; role = (string)dr["name"]; active = (bool)dr["acc_active"]; } } catch { } if (salt != null) { using (var deriveBytes = new Rfc2898DeriveBytes(ppassword, salt)) { byte[] newKey = deriveBytes.GetBytes(192); if (!newKey.SequenceEqual(key)) { return(Tuple.Create(user_id, false, role)); } else { postgres p = new postgres(); p.SqlNonQuery("UPDATE public.users SET last_login=@d WHERE user_id = @user_id;", postgres.list = new List <NpgsqlParameter>() { new NpgsqlParameter("@d", DateTime.Now), new NpgsqlParameter("@user_id", user_id) }); return(Tuple.Create(user_id, true, role)); } } } else { return(Tuple.Create(user_id, false, role)); } }
public List <Receptmodels> GetFoodListForReceptView(string psql, int pid_user, string search) { postgres m = new postgres(); DataTable dt = new DataTable(); List <Receptmodels> mt = new List <Receptmodels>(); if (!string.IsNullOrEmpty(search)) { string modal = "%"; dt = m.SqlQuery(psql, postgres.list = new List <NpgsqlParameter>() { new NpgsqlParameter("@id_user", pid_user), new NpgsqlParameter("@search", modal += search += "%") }); } else { dt = m.SqlQuery(psql, postgres.list = new List <NpgsqlParameter>() { new NpgsqlParameter("@id_user", pid_user) }); } foreach (DataRow dr in dt.Rows) { Receptmodels r = new Receptmodels(); r.Id = Convert.ToInt16(dr["id_recept"].ToString()); r.Name = dr["name"].ToString(); r.Description = (string)dr["description"]; r.Url_pic = (string)dr["url_pic"]; r.Url_recept = (string)dr["url_recept"]; r.cookingtime = (string)dr["time_name"]; r.TypeOfFood = (string)dr["type_name"]; r.Occasions = dr["occasion_id"].ToString(); r.Rating = (double)dr["average_rating"]; mt.Add(r); } return(mt); }
public bool Forgetpassword(string username) { postgres sql = new postgres(); DataTable dt = new DataTable(); string query; if (username.Contains("@")) { query = "SELECT login_id, username, email, acc_active FROM users WHERE email = @username;"; } else { query = "SELECT login_id, username, email, acc_active FROM users WHERE username = @username;"; } dt = sql.SqlQuery(query, postgres.list = new List <NpgsqlParameter>() { new NpgsqlParameter("@username", username) }); Users r = new Users(); foreach (DataRow dr in dt.Rows) { r.User = dr["username"].ToString(); r.email = (string)dr["email"]; r.active = (bool)dr["acc_active"]; r.Login_id = (int)dr["login_id"]; } if (string.IsNullOrEmpty(r.User) || !r.active) { return(false); } string hash = GeneratePassword(60); postgres sql1 = new postgres(); DateTime date = DateTime.UtcNow; date = date.AddHours(3); sql1.SqlNonQuery("UPDATE login set reset_hash=@hash , reset_time =@time WHERE login_id=@id ", postgres.list = new List <NpgsqlParameter>() { new NpgsqlParameter("@id", r.Login_id), new NpgsqlParameter("@hash", hash), new NpgsqlParameter("@time", date) }); string message = Email.EmailOther(r.User, "Här kommer din återställningslänk: <a href=\"https://matslumpiator.se/Account/Resetpassword?validate=" + hash + "\" target=\"_blank\" >Återställlösenordet </a>"); Email.SendEmail(r.email, r.User, "Återställning Lösenord", message); return(true); }