Ejemplo n.º 1
0
 public IActionResult Login(LogRegModels model)
 {
     if (ModelState.IsValid)
     {
         string FormPassword = model.Log.Password;
         string query        = $"SELECT * FROM user WHERE email = '{model.Log.Email}'";
         List <Dictionary <string, object> > user = new List <Dictionary <string, object> >();
         try
         {
             user = DbConnector.Query(query);
             if (user.Count == 0)
             {
                 TempData["ValError"] = "This email does not exist.";
                 return(RedirectToAction("Index"));
             }
         }
         catch (Exception e)
         {
             Debug.Print("Exception when querying for user:"******"password"].ToString(), model.Log.Password) != 0)
         {
             try
             {
                 object        id       = DbConnector.Query($"SELECT iduser FROM user WHERE email = '{model.Log.Email}'")[0]["iduser"];
                 string        idString = id.ToString();
                 CookieOptions option   = new CookieOptions();
                 option.Expires = DateTime.Now.AddMinutes(60);
                 Response.Cookies.Append("userCookie", idString, option);
             }
             catch (Exception e)
             {
                 Debug.Print("Exception when baking cookie: " + e.Message);
             }
             HttpContext.Session.SetString("userEmail", model.Log.Email);
             return(RedirectToAction("Home"));
         }
         else
         {
             TempData["ValError"] = "Incorrect password.";
             return(RedirectToAction("Index"));
         }
     }
     return(View("Index"));
 }
Ejemplo n.º 2
0
 public IActionResult Register(LogRegModels model)
 {
     if (ModelState.IsValid)
     {
         try
         {
             List <Dictionary <string, object> > user = DbConnector.Query($"SELECT * FROM user WHERE email = '{model.Reg.Email}'");
             if (user.Count > 0)
             {
                 TempData["ValError"] = "This email already exists.";
                 return(RedirectToAction("Index"));
             }
         }
         catch (Exception e)
         {
             Debug.Print("Exception when querying for user: "******"INSERT INTO user(Firstname, Lastname, Email, Password, createdAt, updatedAt) VALUES('{model.Reg.Firstname}', '{model.Reg.Lastname}', '{model.Reg.Email}', '{hashedPassword}', '{DateTime.Now.ToString()}', '{DateTime.Now.ToString()}')");
             try
             {
                 string        id     = DbConnector.Query($"SELECT iduser FROM user WHERE email = '{model.Reg.Email}'")[0]["iduser"].ToString();
                 CookieOptions option = new CookieOptions();
                 option.Expires = DateTime.Now.AddMinutes(60);
                 Response.Cookies.Append("userCookie", id, option);
             }
             catch (Exception e)
             {
                 Debug.Print("Exception when baking cookie: " + e.Message);
             }
             HttpContext.Session.SetString("userEmail", model.Reg.Email);
             return(Redirect("Home"));
         }
         catch (Exception e)
         {
             Debug.Print("Exception when inserting new user: "******"Index"));
 }