public ActionResult Register(string Username = "", string Password = "", string Email = "") { if (Username == string.Empty || Password == string.Empty || Email == string.Empty) { return(RedirectToAction("Register", new { errorCode = 100 })); } if (!Regex.Match(Email, "\\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}\\b").Success) { return(RedirectToAction("Register", new { errorCode = 200 })); } // Check for existing username var accountDB = new Models.AccountsDataContext(); var exisiting = (from a in accountDB.Accounts where a.Username == Username select true).ToArray(); if (exisiting.Length > 0) { return(RedirectToAction("Register", new { errorCode = 300 })); } var LoginHash = Util.GenerateMD5(string.Format("{0}{1}", Username, Password)); accountDB.Accounts.InsertOnSubmit(new Models.Account { Username = Username, Password = Core.Util.GenerateMD5(Password), Email = Email, LoginHash = LoginHash }); accountDB.SubmitChanges(); return(RedirectToAction("LogIn")); }
public ActionResult LogIn(string Username = "", string Password = "") { if (Username == "" || Password == "") { return(RedirectToAction("LogIn", new { errorCode = 100 })); } var accountDB = new Models.AccountsDataContext(); var loginHash = (from a in accountDB.Accounts where (a.Username.ToLower() == Username.ToLower()) && (a.Password == Core.Util.GenerateMD5(Password)) select a.LoginHash).ToArray(); if (loginHash.Length == 0) { return(RedirectToAction("LogIn", new { errorCode = 500 })); } var cookie = new HttpCookie("loginHash"); cookie.Value = loginHash[0]; cookie.Expires = DateTime.Now.AddDays(365); Response.Cookies.Add(cookie); return(RedirectToAction("Index")); }
private string cardDbPath = "~/Content/xml/cards.xml"; // The path to the XML Card Database on the server #endregion Fields #region Methods /// <summary> /// GET /Deck/Create/[id] /// The deck editor /// </summary> /// <param name="id">The id of the deck to edit, or -1 to create a new deck.</param> /// <returns>The view.</returns> public ActionResult Create(int id = -1) { if(!Models.AccountUtils.isLoggedIn(this)) return RedirectToAction("LogIn", "Account"); CardDatabase db = new CardDatabase(Server.MapPath(cardDbPath)); var model = new Models.DeckCreateViewModel(); List<string> deckCardList = new List<string>(); if(id >= 0) { var accountDB = new Models.AccountsDataContext(); var loginHash = Request.Cookies["loginHash"].Value; var accountId = (from a in accountDB.Accounts where a.LoginHash == loginHash select a.Id).ToArray(); if(accountId.Length <= 0) return RedirectToAction("LogOut", "Account"); // Check if this account is the owner of the deck var deckDB = new Models.DecksDataContext(); var deck = (from d in deckDB.Decks where (d.Id == id) && (d.OwnerId == accountId[0]) select d).ToArray(); if(deck.Length <= 0) { // This account does not own the deck return RedirectToAction("Create", "Deck", new { id = -1 }); } model.DeckName = deck[0].Name; var cardDB = new Models.CardsDataContext(); var cards = from c in cardDB.Cards where c.DeckId == id select c.CardName; foreach(var card in cards) { deckCardList.Add(card); } } model.deckCards = new List<Core.Card>(); model.deckCards = (from c in deckCardList orderby c select db.GetCardByName(c) ).ToList(); model.sets = db.sets; model.DeckId = id; return View(model); }
public static int GetAccountID(string loginHash) { if (loginHash == null) { return(-1); } if (loginHash == string.Empty) { return(-1); } var accountDB = new Models.AccountsDataContext(); var account = (from a in accountDB.Accounts where a.LoginHash == loginHash select a.Id).ToArray(); if (account.Length <= 0) { return(-1); } return(account[0]); }
public static bool isLoggedIn(Controller controller) { var cookie = controller.Request.Cookies["loginHash"]; if (cookie == null) { return(false); } var accountDB = new Models.AccountsDataContext(); var account = (from x in accountDB.Accounts where x.LoginHash == cookie.Value select true).ToArray(); if (account.Length > 0) { return(true); } else { return(false); } }
/// <summary> /// GET /Deck/Create/[id] /// The deck editor /// </summary> /// <param name="id">The id of the deck to edit, or -1 to create a new deck.</param> /// <returns>The view.</returns> public ActionResult Create(int id = -1) { if (!Models.AccountUtils.isLoggedIn(this)) { return(RedirectToAction("LogIn", "Account")); } CardDatabase db = new CardDatabase(Server.MapPath(cardDbPath)); var model = new Models.DeckCreateViewModel(); List <string> deckCardList = new List <string>(); if (id >= 0) { var accountDB = new Models.AccountsDataContext(); var loginHash = Request.Cookies["loginHash"].Value; var accountId = (from a in accountDB.Accounts where a.LoginHash == loginHash select a.Id).ToArray(); if (accountId.Length <= 0) { return(RedirectToAction("LogOut", "Account")); } // Check if this account is the owner of the deck var deckDB = new Models.DecksDataContext(); var deck = (from d in deckDB.Decks where (d.Id == id) && (d.OwnerId == accountId[0]) select d).ToArray(); if (deck.Length <= 0) { // This account does not own the deck return(RedirectToAction("Create", "Deck", new { id = -1 })); } model.DeckName = deck[0].Name; var cardDB = new Models.CardsDataContext(); var cards = from c in cardDB.Cards where c.DeckId == id select c.CardName; foreach (var card in cards) { deckCardList.Add(card); } } model.deckCards = new List <Core.Card>(); model.deckCards = (from c in deckCardList orderby c select db.GetCardByName(c) ).ToList(); model.sets = db.sets; model.DeckId = id; return(View(model)); }