public static bool SendVerificationEmail(string UserID) { try { using (var db = new MemberLiteEntities().Init) { var u = db.Users.Select(a => new { a.UserID, a.FirstName, a.OtherNames, a.Email, a.VerificationCode }) .Where(a => a.UserID == UserID) .FirstOrDefault(); if (u == null) { ReturnMessage = "Invalid user"; return(false); } string link = new Uri(string.Format(AppConfig.Url + "access/verifyemail?e={0}&c={1}", u.Email, u.VerificationCode)).AbsoluteUri; string body = File.ReadAllText(AppUtility.AppDataPath + "/MailTemplates/EmailVerification.htm"); body = body.Replace("{site_name}", AppConfig.Name); body = body.Replace("{site_url}", AppConfig.Url); body = body.Replace("{name}", u.OtherNames + " " + u.FirstName); body = body.Replace("{verify_link}", link); body = body.Replace("{email}", u.Email); body = body.Replace("{support_mail}", WebMailer.Support); if (WebMailer.Send(WebMailer.Alert, u.Email, AppConfig.Name + " - Email Verification", body, true)) { ReturnMessage = "Verification message has been sent, please goto your inbox and confirm it now."; return(true); } } } catch (Exception ex) { CustomErrorLogger.Log(ex.Message); } ReturnMessage = "Unable to send verification mail"; return(false); }
public static string HandleEFException(DbEntityValidationException Exception, bool Log = true) { foreach (DbEntityValidationResult item in Exception.EntityValidationErrors) { // Get entry var entry = item.Entry; string entityTypeName = entry.Entity.GetType().Name; // Display or log error messages foreach (DbValidationError subItem in item.ValidationErrors) { string message = string.Format("Error '{0}' occurred in {1} at {2}", subItem.ErrorMessage, entityTypeName, subItem.PropertyName); if (Log) { CustomErrorLogger.Log(message, "500"); } // Rollback changes switch (entry.State) { case EntityState.Added: entry.State = EntityState.Detached; break; case EntityState.Modified: entry.CurrentValues.SetValues(entry.OriginalValues); entry.State = EntityState.Unchanged; break; case EntityState.Deleted: entry.State = EntityState.Unchanged; break; } return(message); } } return(""); }
public static List <CustomErrorLogger> LoadLog() { string file = HttpContext.Current.Server.MapPath("~/App_Data/ErrorLog.xml"); XDocument errorLog = XDocument.Load(file); List <CustomErrorLogger> list = new List <CustomErrorLogger>(); CustomErrorLogger log; foreach (var e in errorLog.Root.Elements()) { log = new CustomErrorLogger { DateStamp = DateTime.Parse(e.Attribute("datetime").Value), Code = e.Attribute("code").Value, Message = e.Attribute("message").Value, Page = e.Attribute("page").Value, IP = e.Attribute("ip").Value }; list.Add(log); } return(list); //.OrderBy(Function(p) p.Key) }
public bool Create() { //First line of defence if (this.Password == "" || this.Password.Length < 5) { ReturnMessage = "Password format is incorrect"; return(false); } if (!NameIsValid(this.FirstName)) { ReturnMessage = "Your name is not valid"; return(false); } if (!NameIsValid(this.OtherNames)) { ReturnMessage = "Your name is not valid"; return(false); } //============================================= try { using (var db = new MemberLiteEntities().Init) { //Validate email var uEmail = db.Users.Select(a => new { a.Email }) .Where(a => a.Email == this.Email) .FirstOrDefault(); if (uEmail != null) { ReturnMessage = "Sorry! This email is already in use"; return(false); } //Generate UserID //USR-{MONTH YEAR JOINED}-{RANDOM} string _userID = ""; createUserID: _userID = string.Join("-", "USR", DateTime.Now.ToString("MM") + DateTime.Now.ToString("yy"), new Random().Next(10, 9000000)); //Check if generated id exist in DB var uID = db.Users.Select(a => new { a.UserID }) .Where(a => a.UserID == _userID) .FirstOrDefault(); //You can generate a simple GUID //Using string _id = Guid.NewGuid().ToString(); if (uID != null) { goto createUserID; } else { this.UserID = _userID; } //Encrypt passkey string userIDHash = Crypto.SHA256Hash(this.UserID); string pwd = Crypto.SHA256Hash(this.Password.ToUpper()); string finalPwd = Crypto.SHA256Hash(userIDHash + pwd); this.VerificationCode = AppUtility.GenerateAlphaNumeric(15); this.Password = finalPwd; this.Status = (int)StatusType.Active; this.EmailConfirmed = false; this.DateStamp = DateTime.Now; db.Users.Add(this); db.SaveChanges(); ReturnMessage = "Account created ok"; return(true); } } catch (DbEntityValidationException ex) { CustomErrorLogger.Log(DBHelper.HandleEFException(ex)); //Users should not see your exception message ReturnMessage = "An error occurred while processing your details. Please try again!"; return(false); } catch (Exception ex) { CustomErrorLogger.Log(ex.InnerException.Message); ReturnMessage = "An error occurred while processing your details. Please try again!"; return(false); } }