//Gets profile details from the database and stores them public bool GetProfileDetails(string username) { int userid = Account.GetUserid(username); if (userid == 0) return false; using (var db = new EquestriaGalleriesDatabase()) { //Getting profile details using the userid. If it cant find it (that shouldent happen) returns false var profilequery = from n in db.Users where n.Userid == userid select n; if (profilequery.Count() == 0) { return false; } ProfileViewModel.DisplayName = db.Displaynames.Where(x => x.Active == 1).Where(x => x.Userid == userid).First().Name; ProfileViewModel.Avatar = db.Avatars.Where(x => x.Hidden == false).Where(x => x.Userid == userid).First().Filename; ProfileViewModel.CommentsGiven = db.Profiles.Where(x => x.Userid == userid).First().CommentsGivenCount; ProfileViewModel.CommentsRecived = db.Profiles.Where(x => x.Userid == userid).First().CommentsRecivedCount; ProfileViewModel.Journals = db.Profiles.Where(x => x.Userid == userid).First().JournalCount; ProfileViewModel.Trust = db.Profiles.Where(x => x.Userid == userid).First().Trust; ProfileViewModel.Uploads = db.Profiles.Where(x => x.Userid == userid).First().UploadCount; ProfileViewModel.Views = db.Profiles.Where(x => x.Userid == userid).First().Viewcount; } return true; }
public static bool AuthenticateCookie() { if (HttpContext.Current.Request.Cookies["EQGLoginCookie"] == null) { IsAuthenticated = false; return false; } var cookie = HttpContext.Current.Request.Cookies["EQGLoginCookie"]; string ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; using (var db = new EquestriaGalleriesDatabase()) { var session = from n in db.Sessions where n.Sessionkey == cookie.Value && n.IPAdress == ip select n; if(session.Count() == 0) { IsAuthenticated = false; Username = ""; cookie.Value = null; cookie.Expires = DateTime.Now.AddYears(-1); return false; } Username = session.First().User.Username; IsAuthenticated = true; } return true; }
public static int GetUserid(string username) { using (var db = new EquestriaGalleriesDatabase()) { var query = from n in db.Users where n.Username == username select n; if (query.Count() == 0) return 0; else return query.First().Userid; } }
public void AddLoginResult(bool result, string ip, string username) { using (var db = new EquestriaGalleriesDatabase()) { db.LoginLogs.Add(new LoginLog { IPAdress = ip, Username = username, Result = result, Datetime = DateTime.Now }); try { db.SaveChanges(); } catch (DbEntityValidationException dbEx) { foreach (var validationErrors in dbEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage); } } } } }
// Register // Checks the inputs then adds them to a database // INPUTS: Userame, Password, Email, dob // OUTPUT: bool (Sucsessfull registration) public bool Register(string username, string password, string email, DateTime dob) { //Checking the inputs are not empty if (username == null) return false; if (password == null) return false; if (email == null) return false; //Checking the username exists if (GetUserid(username) != 0) return false; //Generating the salt and encripting the password with the salt Encryption encrypter = new Encryption(); string salt = encrypter.sha256encrypt(username + saltKey + email); string encriptedpassword = encrypter.sha256encrypt(password + salt); username = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(username); //Adding account details to the database using (var db = new EquestriaGalleriesDatabase()) { db.Users.Add(new User { Username = username, Datetime = DateTime.Now }); db.Passwords.Add(new Password { UserPassword = encriptedpassword, Salt = salt, Lastchanged = DateTime.Now, Lastlogin = DateTime.Now }); db.UserSettings.Add(new UserSetting { PrivateEmail = email, DOB = dob }); db.Profiles.Add(new Profile { }); db.Displaynames.Add(new Displayname { Datetime = DateTime.Now, Name = username }); db.Avatars.Add(new Avatar {Datetime = DateTime.Now, Filename = "default.png", Inuse = true }); //Saving the database db.SaveChanges(); return true; } }
public bool Login(string username, string password, bool rememberme, int userid = 0, bool auth = true) { string ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; if (auth) { if (username == null) return false; if (password == null) return false; userid = GetUserid(username); } if (userid == 0) { AddLoginResult(false, ip, username); return false; } using (var db = new EquestriaGalleriesDatabase()) { var passwordquery = from n in db.Passwords where n.Userid == userid select n; if (passwordquery.Count() == 0) { AddLoginResult(false, ip, username); return false; } Encryption encrypter = new Encryption(); string encriptedpassword = encrypter.sha256encrypt(password + passwordquery.First().Salt); var loginquery = from n in db.Users where n.Username == username && n.Password.UserPassword == encriptedpassword select n; if (loginquery.Count() == 0) { AddLoginResult(false, ip, username); return false; } //Checking that the cookie hasent been deleted if deleted remove the session from database issue new one var cookiequery = from n in db.Sessions where n.Userid == userid select n; if (cookiequery.Count() > 0) { db.Sessions.Remove(db.Sessions.Find(userid)); db.SaveChanges(); } } //Creating the cookie and encripting the data var authTicket = new FormsAuthenticationTicket( 1, userid.ToString(), DateTime.Now, DateTime.Now.AddMinutes(20), rememberme, "", "/" ); string encriptedTicket = FormsAuthentication.Encrypt(authTicket); HttpContext.Current.Response.Cookies.Add(new HttpCookie("EQGLoginCookie", encriptedTicket)); using (var db = new EquestriaGalleriesDatabase()) { db.Sessions.Add(new Session { Datetime = DateTime.Now, IPAdress = ip, Sessionkey = encriptedTicket, Userid = userid }); db.SaveChanges(); } AddLoginResult(true, ip, username); HttpContext.Current.Application["OnlineGuests"] = (int)HttpContext.Current.Application["OnlineGuests"] - 1; HttpContext.Current.Application["OnlineUsers"] = (int)HttpContext.Current.Application["OnlineUsers"] + 1; return true; }