public static bool ChangePassword(string username, string newPassword) { try { string newHashedPassword = PasswordHash.MD5Hash(newPassword); User existingUser = new User(); using (var context = new KioskWebDBEntities()) { existingUser = context.Users .Where(t => t.Username == username) .FirstOrDefault(); } if (existingUser != null) { existingUser.HashedPassword = newHashedPassword; existingUser.FirstTime = false; using (var context = new KioskWebDBEntities()) { using (var transaction = context.Database.BeginTransaction()) { context.Entry(existingUser).State = EntityState.Modified; context.SaveChanges(); KioskSolutionLibrary.ModelLibrary.EntityFrameworkLibrary.ThirdPartyData.User thirdPartyUser = new KioskSolutionLibrary.ModelLibrary.EntityFrameworkLibrary.ThirdPartyData.User(); thirdPartyUser.UserName = username; if (ThirdPartyDL.UserExists(thirdPartyUser)) ThirdPartyDL.ChangePassword(username, newPassword); transaction.Commit(); } } return true; } else { return false; } } catch (Exception ex) { throw ex; } }
public static User RetrieveUserByUsername(string username) { try { var existingUser = new User(); using (var context = new KioskWebDBEntities()) { existingUser = context.Users .Where(t => t.Username.Equals(username)) .FirstOrDefault(); } return existingUser; } catch (Exception ex) { throw ex; } }
public static bool UserExists(User user) { try { var existingUser = new User(); using (var context = new KioskWebDBEntities()) { existingUser = context.Users .Where(t => t.Username.Equals(user.Username)) .FirstOrDefault(); } if (existingUser == null) return false; else return true; } catch (Exception ex) { throw ex; } }
public static bool Save(User user) { try { string password = user.HashedPassword; user.HashedPassword = PasswordHash.MD5Hash(password); using (var context = new KioskWebDBEntities()) { using (var transaction = context.Database.BeginTransaction()) { context.Users.Add(user); context.SaveChanges(); KioskSolutionLibrary.ModelLibrary.EntityFrameworkLibrary.ThirdPartyData.User thirdPartyUser = new KioskSolutionLibrary.ModelLibrary.EntityFrameworkLibrary.ThirdPartyData.User(); thirdPartyUser.UserName = user.Username; thirdPartyUser.Password = password; thirdPartyUser.UserType = "1"; thirdPartyUser.status = 1; thirdPartyUser.OfficialEmail = user.Email; if (!ThirdPartyDL.UserExists(thirdPartyUser)) ThirdPartyDL.Save(thirdPartyUser); transaction.Commit(); } } return true; } catch (Exception ex) { throw ex; } }
public static bool Update(User user) { try { User existingUser = new User(); using (var context = new KioskWebDBEntities()) { existingUser = context.Users .Where(t => t.ID == user.ID) .FirstOrDefault(); } if (existingUser != null) { existingUser.Email = user.Email; existingUser.Gender = user.Gender; existingUser.PhoneNumber = user.PhoneNumber; existingUser.Lastname = user.Lastname; existingUser.Othernames = user.Othernames; existingUser.UserRole = user.UserRole; existingUser.UserBranch = user.UserBranch; using (var context = new KioskWebDBEntities()) { context.Entry(existingUser).State = EntityState.Modified; context.SaveChanges(); } return true; } else { return false; } } catch (Exception ex) { throw ex; } }
public static bool UserExists(string username, string password) { try { var user = new User(); user = UserDL.AuthenticateUser(username, PasswordHash.MD5Hash(password)); if (user == null) return false; else return true; } catch (Exception ex) { throw ex; } }
public static bool Update(User user) { try { return UserDL.Update(user); } catch (Exception ex) { throw ex; } }
public static bool Save(User user, out string message) { try { if (UserDL.UserExists(user)) { message = string.Format("User with username: {0} exists already", user.Username); return false; } else { message = string.Empty; if (UserDL.Save(user)) { return true; } else return false; } } catch (Exception ex) { throw ex; } }
public static void SendNewUserMail(User user) { try { Role role = RolePL.RetrieveRoleByID(user.UserRole); string userFullName = user.Lastname + " " + user.Othernames; string userUsername = user.Username; string userPassword = user.HashedPassword; string userRole = role.Name; string organization = System.Configuration.ConfigurationManager.AppSettings.Get("Organization"); string applicationName = System.Configuration.ConfigurationManager.AppSettings.Get("ApplicationName"); string websiteUrl = System.Configuration.ConfigurationManager.AppSettings.Get("WebsiteUrl"); string subject = "Welcome to " + applicationName; string userFunction = ""; foreach(RoleFunction roleFunction in role.RoleFunctions) { userFunction += roleFunction.Function.Name + "<br/>"; } string fromAddress = ""; string smtpUsername = ""; string smtpPassword = ""; string smtpHost = ""; Int32 smtpPort = 587; bool smtpUseDefaultCredentials = false; bool smtpEnableSsl = true; MailHelper mailConfig = ConfigurationManager.GetSection("mailHelperSection") as MailHelper; if (mailConfig != null && mailConfig.Mail != null) { fromAddress = mailConfig.Mail.FromEmailAddress; smtpUsername = mailConfig.Mail.Username; smtpPassword = mailConfig.Mail.Password; } if (mailConfig != null && mailConfig.Smtp != null) { smtpHost = mailConfig.Smtp.Host; smtpPort = Convert.ToInt32(mailConfig.Smtp.Port); smtpUseDefaultCredentials = Convert.ToBoolean(mailConfig.Smtp.UseDefaultCredentials); smtpEnableSsl = Convert.ToBoolean(mailConfig.Smtp.EnableSsl); } string body = ""; body = System.IO.File.ReadAllText(System.Web.Hosting.HostingEnvironment.MapPath(@"~/App_Data/MailTemplates/NewUser.txt")); body = body.Replace("#Organization", organization); body = body.Replace("#ApplicationName", applicationName); body = body.Replace("#UserFullName", userFullName); body = body.Replace("#Username", userUsername); body = body.Replace("#Password", userPassword); body = body.Replace("#Role", userRole); body = body.Replace("#UserFunctions", userFunction); body = body.Replace("#WebsiteUrl", websiteUrl); Thread email = new Thread(delegate() { Mail.SendMail(user.Email, fromAddress, subject, body, smtpHost, smtpPort, smtpUseDefaultCredentials, smtpUsername, smtpPassword, smtpEnableSsl); }); email.IsBackground = true; email.Start(); } catch (Exception ex) { ErrorHandler.WriteError(ex); throw ex; } }
public static void SendForgotPasswordMail(User user) { try { string key = System.Configuration.ConfigurationManager.AppSettings.Get("ekey"); string encrypted_username = Crypter.Encrypt(key, user.Username); string userFullName = user.Lastname + " " + user.Othernames; string organization = System.Configuration.ConfigurationManager.AppSettings.Get("Organization"); string applicationName = System.Configuration.ConfigurationManager.AppSettings.Get("ApplicationName"); string websiteUrl = System.Configuration.ConfigurationManager.AppSettings.Get("WebsiteUrl"); string passwordResetUrl = websiteUrl + "User/ResetPassword?rq=" + encrypted_username; ; string subject = "Password Reset Request on " + applicationName; string fromAddress = ""; string smtpUsername = ""; string smtpPassword = ""; string smtpHost = ""; Int32 smtpPort = 587; bool smtpUseDefaultCredentials = false; bool smtpEnableSsl = true; MailHelper mailConfig = ConfigurationManager.GetSection("mailHelperSection") as MailHelper; if (mailConfig != null && mailConfig.Mail != null) { fromAddress = mailConfig.Mail.FromEmailAddress; smtpUsername = mailConfig.Mail.Username; smtpPassword = mailConfig.Mail.Password; } if (mailConfig != null && mailConfig.Smtp != null) { smtpHost = mailConfig.Smtp.Host; smtpPort = Convert.ToInt32(mailConfig.Smtp.Port); smtpUseDefaultCredentials = Convert.ToBoolean(mailConfig.Smtp.UseDefaultCredentials); smtpEnableSsl = Convert.ToBoolean(mailConfig.Smtp.EnableSsl); } string body = ""; body = System.IO.File.ReadAllText(System.Web.Hosting.HostingEnvironment.MapPath(@"~/App_Data/MailTemplates/ForgotPassword.txt")); body = body.Replace("#Organization", organization); body = body.Replace("#ApplicationName", applicationName); body = body.Replace("#UserFullName", userFullName); body = body.Replace("#WebsiteUrl", websiteUrl); body = body.Replace("#PasswordResetUrl", passwordResetUrl); Thread email = new Thread(delegate() { Mail.SendMail(user.Email, fromAddress, subject, body, smtpHost, smtpPort, smtpUseDefaultCredentials, smtpUsername, smtpPassword, smtpEnableSsl); }); email.IsBackground = true; email.Start(); } catch (Exception ex) { ErrorHandler.WriteError(ex); throw ex; } }