public ActionResult PostLogin(User user) { if (ModelState.IsValid) { using (APIEntities1 api = new APIEntities1()) { User usr = api.Users.FirstOrDefault(u => u.username.Equals(user.username) && u.password.Equals(user.password) ); if (usr != null) { string microSeconds = DateTime.Now.ToString("HH:mm:ss.ffffff"); string token = Convert.ToBase64String(Encoding.UTF8.GetBytes(microSeconds)); usr.token = token; api.SaveChanges(); Session.Add("user", new UserDTO { UserId = usr.user_id, Username = usr.username, Token = token }); return(Redirect("~/dashboard")); } } } return(View("Login")); }
public ActionResult Logout() { UserDTO user = (UserDTO)Session["user"]; using (APIEntities1 api = new APIEntities1()) { User u = api.Users.Find(user.UserId); u.token = null; api.SaveChanges(); } Session["user"] = null; return(Redirect("~/login")); }