public static void ResetUserPassword(string username, string newPassword, bool sendEmail, SPWeb web) { if (Utils.BaseMembershipProvider().RequiresQuestionAndAnswer || !Utils.BaseMembershipProvider().EnablePasswordReset) { throw new Exception(LocalizedString.GetGlobalString("FBAPackWebPages", "ResetPasswordUnavailable")); } MembershipUser user = Utils.BaseMembershipProvider().GetUser(username, false); string password = user.ResetPassword(); //Change the password to the specified password if (!String.IsNullOrEmpty(newPassword)) { if (user.ChangePassword(password, newPassword)) { password = newPassword; } else { throw new Exception(LocalizedString.GetGlobalString("FBAPackWebPages", "ResetPasswordChangePasswordError")); } } if (sendEmail) { MembershipRequest request = MembershipRequest.GetMembershipRequest(user, web); request.Password = password; MembershipRequest.SendResetPasswordEmail(request, web); } }
private void SendEmail() { SPSecurity.RunWithElevatedPrivileges(delegate() { using (SPSite _site = new SPSite(SPContext.Current.Site.ID, SPContext.Current.Site.Zone)) { using (SPWeb _web = _site.OpenWeb(SPContext.Current.Web.ID)) { if (_web != null) { _site.AllowUnsafeUpdates = true; _web.AllowUnsafeUpdates = true; if (!SPUtility.IsEmailServerSet(_web)) { TemplateHelper helper = new TemplateHelper(_ctlPasswordRecovery.SuccessTemplateContainer); helper.SetText("Success", "Password can not be reset since email server is not configutred for this sharepoint application."); return; } PasswordRecovery prc = _ctlPasswordRecovery; MembershipUser currentUser = Utils.BaseMembershipProvider(_web.Site).GetUser(prc.UserName, false); MembershipRequest membershipitem = MembershipRequest.GetMembershipRequest(currentUser, _web); membershipitem.PasswordQuestion = currentUser.PasswordQuestion; membershipitem.Password = currentUser.ResetPassword(prc.Answer); if (!MembershipRequest.SendPasswordRecoveryEmail(membershipitem, _web)) { TemplateHelper helper = new TemplateHelper(_ctlPasswordRecovery.SuccessTemplateContainer); helper.SetText("Success", LocalizedString.GetString("FBAPackPasswordRecoveryWebPart", "ErrorSendingEmail")); } } } } }); }