protected override void SaveForm() { MembershipUser user = null; if (CurrentAspNetId.HasValue) { user = Membership.GetUser(CurrentAspNetId.Value); } if (user != null) { if (user.IsLockedOut) { OnCancelled(); } else { string newPassword = OrderNumberGenerator.GenerateOrderNumber("?#?#?#?#"); string tempPassword = user.ResetPassword(); bool success = user.ChangePassword(tempPassword, newPassword); // send email Email.EmailController ec = new Email.EmailController(); ec.SendMail_PasswordReset(user.Email, newPassword); OnSaved(new ControlSavedEventArgs(CurrentAspNetId)); } } }
void ResetPassword(string emailAddress) { string userName = string.Empty; MembershipUser user = null; bool success = false; string newPassword = string.Empty; userName = Membership.GetUserNameByEmail(emailAddress); if (!string.IsNullOrWhiteSpace(userName)) { user = Membership.GetUser(userName); if (user != null) { string tempPassword = user.ResetPassword(); newPassword = OrderNumberGenerator.GenerateOrderNumber("?#?#?#?#"); success = user.ChangePassword(tempPassword, newPassword); } } if (success) { // send email EmailController ec = new EmailController(); ec.SendMail_PasswordReset(emailAddress, newPassword); lblFeedback.Text = "<span style='color:green;'>Password Reset Successful - Your new password has been sent to the email address: " + emailAddress + "</span><br/><br/><a href='/login.aspx'>Click Here</a> to return to account log in screen.</span>"; divForm.Visible = false; } else { lblFeedback.Text = "<span style='color:red;'>Password Reset Failed. Email address not recognized. " + "Please re-enter your email address or call customer service at 866-575-2433 for assistance.</span>"; } }
public static void Import(out int count) { using (StreamWriter writer = new StreamWriter(@"C:\HCCCustomerOutput.txt")) { Console.SetOut(writer); count = 0; List <string> errorReport = new List <string>(); try { List <ImportedCustomer> impCusts = GetAll(); foreach (ImportedCustomer impCust in impCusts) { Console.WriteLine(count + " : "); if (!impCust.IsValid) { impCust.Email = "admin" + count.ToString() + "@healthychefcreations.com"; } else if (impCust.Email.Contains("info@healthychef") || impCust.Email.Contains("thehealthyassistant@earthlink")) { impCust.Email = "admin" + count.ToString() + "@healthychefcreations.com"; } Console.WriteLine(impCust.Email); if (impCust.IsValid) { count++; string userName = impCust.Email.Trim().Split('@')[0] + DateTime.Now.ToString("yyyyMMddHHmmtt"); string password = OrderNumberGenerator.GenerateOrderNumber("?#?#?#?#"); string aspUserName = Membership.GetUserNameByEmail(impCust.Email.Trim()); MembershipUser newUser = null; if (!string.IsNullOrWhiteSpace(aspUserName)) { newUser = Membership.GetUser(aspUserName); } MembershipCreateStatus createResult = MembershipCreateStatus.UserRejected; if (newUser == null) { newUser = Membership.CreateUser(userName, password, impCust.Email.Trim(), "import", "import", true, out createResult); if (newUser != null) { Console.WriteLine(newUser.UserName + "New user."); } } else { Console.WriteLine(newUser.UserName + " Existing user."); createResult = MembershipCreateStatus.Success; } if (newUser != null) { if (createResult == MembershipCreateStatus.Success) { //Assign Customer role to newUser try { if (!Roles.IsUserInRole(newUser.UserName, "Customer")) { Roles.AddUserToRole(newUser.UserName, "Customer"); Console.WriteLine(newUser.UserName + " Role assigned."); } } catch (Exception ex) { Console.WriteLine(newUser.UserName + " =Assign role failed." + ex.Message + ex.StackTrace); } //Send E-mail notification to account user //HealthyChef.Email.EmailController ec = new HealthyChef.Email.EmailController(); //ec.SendMail_NewUserConfirmation(email, password); //Create a Healthy Chef profile for this new user hccUserProfile newProfile = hccUserProfile.GetBy((Guid)newUser.ProviderUserKey).SingleOrDefault(a => !a.ParentProfileID.HasValue); if (newProfile == null) { try { newProfile = new hccUserProfile { MembershipID = (Guid)newUser.ProviderUserKey, CreatedBy = (Membership.GetUser() == null ? Guid.Empty : (Guid)Membership.GetUser().ProviderUserKey), CreatedDate = DateTime.Now, AccountBalance = 0.00m, IsActive = true, FirstName = impCust.FirstName.Trim(), LastName = impCust.LastName.Trim(), ProfileName = impCust.FirstName.Trim() }; //Save all hccProfile information using (var cont = new healthychefEntities()) { System.Data.EntityKey key = cont.CreateEntityKey("hccUserProfiles", newProfile); object oldObj; if (cont.TryGetObjectByKey(key, out oldObj)) { cont.ApplyCurrentValues("hccUserProfiles", newProfile); } else { cont.hccUserProfiles.AddObject(newProfile); } cont.SaveChanges(); } //cont.Refresh(System.Data.Objects.RefreshMode.StoreWins, newProfile); Console.WriteLine(newUser.UserName + " New profile."); } catch (Exception) { Console.WriteLine("=" + newUser.UserName + " Save Profile failed."); } } else { Console.WriteLine(newUser.UserName + " Existing profile."); createResult = MembershipCreateStatus.Success; } if (newProfile != null && newProfile.UserProfileID > 0) { if (impCust.OtherDeliveryInfo != null && !string.IsNullOrWhiteSpace(impCust.OtherDeliveryInfo)) { hccUserProfileNote shipNote = new hccUserProfileNote { DateCreated = DateTime.Now, DisplayToUser = false, UserProfileID = newProfile.UserProfileID, IsActive = true, Note = impCust.OtherDeliveryInfo, NoteTypeID = (int)Enums.UserProfileNoteTypes.ShippingNote }; using (var cont = new healthychefEntities()) { EntityKey key = cont.CreateEntityKey("hccUserProfileNotes", shipNote); object originalItem = null; if (cont.TryGetObjectByKey(key, out originalItem)) { cont.ApplyCurrentValues(key.EntitySetName, shipNote); } else { cont.hccUserProfileNotes.AddObject(shipNote); } cont.SaveChanges(); } } if (impCust.HowDidYouHear != null && !string.IsNullOrWhiteSpace(impCust.HowDidYouHear)) { hccUserProfileNote hearNote = new hccUserProfileNote { DateCreated = DateTime.Now, DisplayToUser = false, UserProfileID = newProfile.UserProfileID, IsActive = true, Note = impCust.HowDidYouHear, NoteTypeID = (int)Enums.UserProfileNoteTypes.GeneralNote }; using (var cont = new healthychefEntities()) { EntityKey key = cont.CreateEntityKey("hccUserProfileNotes", hearNote); object originalItem = null; if (cont.TryGetObjectByKey(key, out originalItem)) { cont.ApplyCurrentValues(key.EntitySetName, hearNote); } else { cont.hccUserProfileNotes.AddObject(hearNote); } cont.SaveChanges(); } } try { //save Shipping Address hccAddress shipAddr = null; if (newProfile.ShippingAddressID.HasValue) { shipAddr = hccAddress.GetById(newProfile.ShippingAddressID.Value); } if (shipAddr != null) { try { newProfile.ShippingAddressID = null; //Save all hccProfile information using (var cont = new healthychefEntities()) { System.Data.EntityKey key1 = cont.CreateEntityKey("hccUserProfiles", newProfile); object oldObj1; if (cont.TryGetObjectByKey(key1, out oldObj1)) { cont.ApplyCurrentValues("hccUserProfiles", newProfile); } else { cont.hccUserProfiles.AddObject(newProfile); } cont.SaveChanges(); } //cont.Refresh(System.Data.Objects.RefreshMode.StoreWins, newProfile); using (var cont = new healthychefEntities()) { EntityKey key = cont.CreateEntityKey("hccAddresses", shipAddr); object originalItem = null; if (cont.TryGetObjectByKey(key, out originalItem)) { cont.AttachTo(shipAddr.EntityKey.EntitySetName, shipAddr); cont.DeleteObject(shipAddr); } cont.SaveChanges(); } shipAddr = null; } catch (Exception ex) { Console.WriteLine(newUser.UserName + " =Delete old shipping address failed." + ex.Message + ex.StackTrace); } } if (shipAddr == null) { shipAddr = new hccAddress(); } if (impCust.ShippingAddress1 != null) { shipAddr.Address1 = (string.IsNullOrWhiteSpace(impCust.ShippingAddress1) ? "" : impCust.ShippingAddress1.Trim()); } else { shipAddr.Address1 = ""; } if (impCust.ShippingAddress2 != null) { shipAddr.Address2 = (string.IsNullOrWhiteSpace(impCust.ShippingAddress2) ? "" : impCust.ShippingAddress2.Trim()); } else { shipAddr.Address2 = ""; } if (impCust.ShippingAddress3 != null) { shipAddr.Address2 += " " + (string.IsNullOrWhiteSpace(impCust.ShippingAddress3) ? "" : impCust.ShippingAddress3.Trim()); } shipAddr.AddressTypeID = (int)Enums.AddressType.Shipping; if (impCust.ShippingCity != null) { shipAddr.City = (string.IsNullOrWhiteSpace(impCust.ShippingCity) ? "" : impCust.ShippingCity.Trim()); } else { shipAddr.City = ""; } shipAddr.Country = "US"; if (impCust.FirstName != null) { shipAddr.FirstName = (string.IsNullOrWhiteSpace(impCust.FirstName) ? "" : impCust.FirstName.Trim()); } else { shipAddr.FirstName = ""; } shipAddr.IsBusiness = false; if (impCust.LastName != null) { shipAddr.LastName = (string.IsNullOrWhiteSpace(impCust.LastName) ? "" : impCust.LastName.Trim()); } else { shipAddr.LastName = ""; } if (impCust.Phone1 != null) { shipAddr.Phone = (string.IsNullOrWhiteSpace(impCust.Phone1) ? "" : impCust.Phone1.Trim()); } else { shipAddr.Phone = ""; } if (impCust.Phone1Ext != null) { shipAddr.Phone += (string.IsNullOrWhiteSpace(impCust.Phone1Ext.Trim()) ? "" : " x" + impCust.Phone1Ext.Trim()); } else { shipAddr.Phone = ""; } if (impCust.ShippingZipCode != null) { shipAddr.PostalCode = (string.IsNullOrWhiteSpace(impCust.ShippingZipCode) ? "" : impCust.ShippingZipCode.Trim()); } else { shipAddr.PostalCode = ""; } if (impCust.ShippingState != null) { shipAddr.State = (string.IsNullOrWhiteSpace(impCust.ShippingState) ? "" : impCust.ShippingState.Trim()); } else { shipAddr.State = ""; } if (impCust.ShipMethod == null) { shipAddr.DefaultShippingTypeID = (int)Enums.DeliveryTypes.Delivery; } else if (impCust.ShipMethod.Trim() == "F") { shipAddr.DefaultShippingTypeID = (int)Enums.DeliveryTypes.Delivery; } else if (impCust.ShipMethod.Trim() == "P") { shipAddr.DefaultShippingTypeID = (int)Enums.DeliveryTypes.LocalPickUp; } else if (impCust.ShipMethod.Trim() == "D") { shipAddr.DefaultShippingTypeID = (int)Enums.DeliveryTypes.LocalDelivery; } else { shipAddr.DefaultShippingTypeID = (int)Enums.DeliveryTypes.Delivery; } if (shipAddr != null) { try { using (var cont = new healthychefEntities()) { EntityKey key = cont.CreateEntityKey("hccAddresses", shipAddr); object originalItem = null; if (cont.TryGetObjectByKey(key, out originalItem)) { cont.hccAddresses.ApplyCurrentValues((hccAddress)originalItem); } else { cont.hccAddresses.AddObject(shipAddr); } cont.SaveChanges(); } //cont.Refresh(System.Data.Objects.RefreshMode.StoreWins, shipAddr); } catch (Exception ex) { Console.WriteLine(newUser.UserName + " =Shipping address save failed." + ex.Message + ex.StackTrace); } } if (shipAddr != null && shipAddr.AddressID > 0) { newProfile.ShippingAddressID = shipAddr.AddressID; } else { newProfile.ShippingAddressID = null; } using (var cont = new healthychefEntities()) { System.Data.EntityKey upkey = cont.CreateEntityKey("hccUserProfiles", newProfile); object oldObj; if (cont.TryGetObjectByKey(upkey, out oldObj)) { cont.ApplyCurrentValues("hccUserProfiles", newProfile); } else { cont.hccUserProfiles.AddObject(newProfile); } cont.SaveChanges(); } Console.WriteLine(newUser.UserName + " Shipping address saved."); } catch (Exception ex) { Console.WriteLine(newUser.UserName + " =Shipping address not created." + ex.Message + ex.StackTrace); } } else { Console.WriteLine("User Profile for user: "******" ID not created."); } } else { Console.WriteLine("=New user for user: "******" not created."); } } else { Console.WriteLine(createResult.ToString() + " : " + impCust.Email); } } else { count++; Console.WriteLine("=Customer: " + impCust.FirstName + " " + impCust.LastName + " has no email address."); } } } catch (Exception ex) { Console.WriteLine("=" + ex.Message + " : " + ex.StackTrace); } } }
protected override void SaveForm() { try { MembershipUser user = null; if (CurrentAspNetId != null) { user = Membership.GetUser(CurrentAspNetId); } else { try { user = Membership.GetUser(Membership.GetUserNameByEmail(txtEmail.Text.Trim())); } catch (Exception) { } } if (user == null) // create new profile { // create membership user //formulate username string email = txtEmail.Text.Trim(); string userName = email.Split('@')[0] + DateTime.Now.ToString("yyyyMMddHHmmtt"); string password = OrderNumberGenerator.GenerateOrderNumber("?#?#?#?#"); MembershipCreateStatus createResult; MembershipUser newUser = Membership.CreateUser(userName, password, email, null, null, true, out createResult); List <ListItem> selRoles = cblRoles.Items.OfType <ListItem>().Where(a => a.Selected).ToList(); selRoles.ForEach(delegate(ListItem item) { Roles.AddUserToRole(userName, item.Value); }); if (createResult == MembershipCreateStatus.Success) { CurrentAspNetId = (Guid)newUser.ProviderUserKey; Email.EmailController ec = new Email.EmailController(); ec.SendMail_NewUserConfirmation(email, password); if (selRoles.Where(a => a.Text.Contains("Customer")).Count() > 0) { hccUserProfile newProfile = new hccUserProfile { MembershipID = (Guid)newUser.ProviderUserKey, CreatedBy = (Guid)Helpers.LoggedUser.ProviderUserKey, CreatedDate = DateTime.Now, ProfileName = txtProfileName.Text.Trim(), FirstName = txtFirstName.Text.Trim(), LastName = txtLastName.Text.Trim(), IsActive = true }; if (ddlCoupons.SelectedIndex > 0) { newProfile.DefaultCouponId = int.Parse(ddlCoupons.SelectedValue); } else { newProfile.DefaultCouponId = null; } newProfile.Save(); CurrentUserID.Value = newProfile.MembershipID.ToString(); this.PrimaryKeyIndex = newProfile.UserProfileID; Response.Redirect("~/WebModules/ShoppingCart/Admin/AccountManager.aspx?UserID=" + newProfile.MembershipID.ToString(), false); } liBilling.Visible = true; liShipping.Visible = true; liPrefs.Visible = true; liAllergens.Visible = true; liSubProfiles.Visible = true; liNotes.Visible = true; liTransactions.Visible = true; liPurchases.Visible = true; liCart.Visible = true; tabs2.Visible = true; tabs3.Visible = true; tabs4.Visible = true; tabs5.Visible = true; tabs6.Visible = true; tabs8.Visible = true; tabs10.Visible = true; tabs7.Visible = true; tabs9.Visible = true; LoadForm(); OnSaved(new ControlSavedEventArgs(newUser.ProviderUserKey)); } else { cstValProfile0.Enabled = true; cstValProfile0.ErrorMessage = Helpers.CreateUserStatusMessage(createResult); cstValProfile0.Validate(); Page.Validate(); } } else // edit existing profile { if (user.Email != txtEmail.Text.Trim()) // update userprofile and aspmembership user { user.Email = txtEmail.Text.Trim(); Membership.UpdateUser(user); } if (chkIsLockedOut.Checked) { Helpers.LockUser(user); } else { if (user.IsLockedOut) { user.UnlockUser(); } if (!user.IsApproved) { user.IsApproved = true; Membership.UpdateUser(user); } } List <ListItem> selRoles = cblRoles.Items.OfType <ListItem>().Where(a => a.Selected).ToList(); if (Roles.IsUserInRole(Helpers.LoggedUser.UserName, "Administrators")) { Roles.GetAllRoles().ToList().ForEach(delegate(string roleName) { if (Roles.IsUserInRole(user.UserName, roleName)) { Roles.RemoveUserFromRole(user.UserName, roleName); } }); selRoles.ForEach(delegate(ListItem item) { Roles.AddUserToRole(user.UserName, item.Value); }); } hccUserProfile editProfile = hccUserProfile.GetParentProfileBy((Guid)user.ProviderUserKey); if (editProfile == null && selRoles.Where(a => a.Text.Contains("Customer")).Count() > 0) { editProfile = new hccUserProfile { MembershipID = (Guid)user.ProviderUserKey, CreatedBy = (Guid)Helpers.LoggedUser.ProviderUserKey, CreatedDate = DateTime.Now, ProfileName = txtProfileName.Text.Trim(), FirstName = txtFirstName.Text.Trim(), LastName = txtLastName.Text.Trim(), CanyonRanchCustomer = cbCanyonRanchCustomer.Checked }; editProfile.Save(); this.PrimaryKeyIndex = editProfile.UserProfileID; OnSaved(new ControlSavedEventArgs(editProfile.UserProfileID)); } if (editProfile != null) { editProfile.ProfileName = txtProfileName.Text.Trim(); editProfile.FirstName = txtFirstName.Text.Trim(); editProfile.LastName = txtLastName.Text.Trim(); editProfile.CanyonRanchCustomer = cbCanyonRanchCustomer.Checked; if (ddlCoupons.SelectedIndex > 0) { editProfile.DefaultCouponId = int.Parse(ddlCoupons.SelectedValue); } else { editProfile.DefaultCouponId = null; } editProfile.Save(); if (!ProfilePrefsEdit1.ShowSave) { ProfilePrefsEdit1.Save(); } if (!ProfileAllgsEdit1.ShowSave) { ProfileAllgsEdit1.Save(); } ProfileCartEdit1.Bind(); OnSaved(new ControlSavedEventArgs(editProfile.UserProfileID)); } OnSaved(new ControlSavedEventArgs(editProfile.UserProfileID)); } } catch (ProviderException pex) { lblFeedback.Text = pex.Message; } catch { throw; } }
protected void btnContinue_Click(object sender, EventArgs e) { lblerror.Text = ""; lblsuccess.Text = ""; var users = (from MembershipUser u in Membership.GetAllUsers() where u.Email == txtForgotEmail.Text.Trim() select new { Email = u.Email }).ToList(); if (users.Count == 0) { lblerror.Text = "This email is not registered"; } else { string userName = string.Empty; MembershipUser user = null; bool success = false; string newPassword = string.Empty; try { userName = Membership.GetUserNameByEmail(txtForgotEmail.Text.Trim()); MembershipUser forgotpassworduser = Membership.GetUser(userName); string[] roles = Roles.GetRolesForUser(userName); if (roles.Contains("Customer") && roles.Count() == 1) { lblerror.Text = "Access denied"; } else { if (!string.IsNullOrWhiteSpace(userName)) { user = Membership.GetUser(userName); if (user != null) { string tempPassword = user.ResetPassword(); newPassword = OrderNumberGenerator.GenerateOrderNumber("?#?#?#?#"); success = user.ChangePassword(tempPassword, newPassword); } if (success) { //send Email EmailController Ec = new EmailController(); Ec.SendMail_PasswordReset(txtForgotEmail.Text.Trim(), newPassword); lblsuccess.Text = "Password Reset Successful - Your new password has been sent to the email address: " + txtForgotEmail.Text.Trim(); } else { lblerror.Text = "Password Reset Failed. Email address not recognized. " + "Please re-enter your email address or call customer service at 866-575-2433 for assistance."; } } } } catch (MembershipPasswordException ex) { lblerror.Text = "Cannot reset password. This account is currently locked."; } catch (Exception exstring) { lblerror.Text = exstring.Message; } } }