public string ResetClientPassword(string email) { try { if (string.IsNullOrWhiteSpace(email)) return "Email cannot be empty!"; email = email.Trim().ToLower(); using (var db = new LomsContext()) { //check id user with such email existed already var existedUser = (from u in db.AssociationUsers where u.AssociationId == CurrentAssociationId && u.Email == email select u).SingleOrDefault(); if (existedUser == null) return "Not a registered user!"; if (!existedUser.HasOnlineAccess) return "Not a with online access!"; var pwdReset = db.AssociationUserPasswordResets.SingleOrDefault(r => r.AssociationUserId == existedUser.Id); if (pwdReset == null) { pwdReset = new AssociationUserPasswordReset(); pwdReset.AssociationUserId = existedUser.Id; } pwdReset.Guid = Guid.NewGuid(); pwdReset.Time = DateTime.UtcNow.AddHours(2.0); db.AssociationUserPasswordResets.ApplyChanges(pwdReset); db.SaveChanges(); var emailProvider = db.AssociationEmails.FirstOrDefault(e => e.AssociationId == CurrentAssociationId); if (emailProvider != null) { var association = db.Associations.FirstOrDefault(a => a.Id == CurrentAssociationId); var uri = HttpContext.Current.Request.Url; string baseUrl = String.Format("{0}://{1}:{2}", uri.Scheme, uri.Host ?? "80", uri.Port); string activtionLink = Path.Combine(baseUrl + string.Format("/#PasswordReset/{0}", pwdReset.Guid.ToString("D"))); string contactUsLink = Path.Combine(baseUrl + "/#Contact"); var emailTemplate = new EmailTemplate("PasswordReset"); emailTemplate["UserName"] = existedUser.FullName.ToUpper(); emailTemplate["ActivationLink"] = activtionLink; emailTemplate["ContactUsLink"] = contactUsLink; var avBody = AlternateView.CreateAlternateViewFromString(emailTemplate.Html, null, MediaTypeNames.Text.Html); emailProvider.SendMail(existedUser.Email, association.Name + " - Reset you Password", emailTemplate.Txt, null, avBody, true); } return ""; } } catch (Exception ex) { StringBuilder builder = new StringBuilder(); builder.AppendLine(ex.Message); if (ex.InnerException != null) { builder.AppendLine(ex.InnerException.Message); if (ex.InnerException.InnerException != null) builder.AppendLine(ex.InnerException.InnerException.Message); } return builder.ToString(); } }
public bool Equals(AssociationUserPasswordReset other) { if (ReferenceEquals(null, other)) return false; if (ReferenceEquals(this, other)) return true; if (other.AssociationUserId == 0 && AssociationUserId == 0) return false; else return other.AssociationUserId == AssociationUserId; }