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); } }
protected void OnResetPassword(object sender, EventArgs e) { string username = Request.QueryString["USERNAME"]; bool sendEmail = true; string newPassword = null; if (resetSelectPassword.Checked) { newPassword = txtNewPassword.Text; sendEmail = chkSendEmail.Checked; } try { Utils.ResetUserPassword(username, newPassword, sendEmail, Web); } catch (ArgumentException ex) { lblNewPasswordError.Text = ex.Message; return; } catch (Exception ex) { Utils.LogError(ex, true); SPUtility.TransferToErrorPage(LocalizedString.GetGlobalString("FBAPackWebPages", "UnexpectedError")); return; } SPUtility.Redirect("FBA/Management/UsersDisp.aspx", SPRedirectFlags.RelativeToLayoutsPage | SPRedirectFlags.UseSource, this.Context); }
protected override void OnLoad(EventArgs e) { this.CheckRights(); // display error confirmation message string userName = Request.QueryString["USERNAME"]; if (string.IsNullOrEmpty(userName)) { SPUtility.TransferToErrorPage(LocalizedString.GetGlobalString("FBAPackWebPages", "UserNotFound")); return; } if (Utils.BaseMembershipProvider().RequiresQuestionAndAnswer || !Utils.BaseMembershipProvider().EnablePasswordReset) { SPUtility.TransferToErrorPage(LocalizedString.GetGlobalString("FBAPackWebPages", "ResetPasswordUnavailable")); return; } resetPasswordMsg.Text = string.Format(LocalizedString.GetGlobalString("FBAPackWebPages", "ResetPasswordMsg"), userName); lblNewPasswordError.Text = ""; if (!this.Page.IsPostBack) { resetAutoPassword.Checked = true; resetSelectPassword.Checked = false; chkSendEmail.Checked = true; } }
protected void SetErrorMessage(MembershipCreateStatus status) { switch (status) { case MembershipCreateStatus.DuplicateUserName: lblMessage.Text = LocalizedString.GetGlobalString("FBAPackWebPages", "DuplicateUserName"); break; case MembershipCreateStatus.DuplicateEmail: lblEmailMessage.Text = LocalizedString.GetGlobalString("FBAPackWebPages", "DuplicateEmail"); break; case MembershipCreateStatus.InvalidPassword: string message = ""; if (string.IsNullOrEmpty(Utils.BaseMembershipProvider().PasswordStrengthRegularExpression)) { message = string.Format(LocalizedString.GetGlobalString("FBAPackWebPages", "InvalidPasswordChars"), Utils.BaseMembershipProvider().MinRequiredPasswordLength, Utils.BaseMembershipProvider().MinRequiredNonAlphanumericCharacters); } else { message = string.Format(LocalizedString.GetGlobalString("FBAPackWebPages", "InvalidPasswordCharsRegex"), Utils.BaseMembershipProvider().MinRequiredPasswordLength, Utils.BaseMembershipProvider().MinRequiredNonAlphanumericCharacters, Utils.BaseMembershipProvider().PasswordStrengthRegularExpression); } //LocalizedString.GetGlobalString("FBAPackWebPages", "InvalidPassword") // TODO: use resource files lblPasswordMessage.Text = message; break; case MembershipCreateStatus.InvalidEmail: lblEmailMessage.Text = LocalizedString.GetGlobalString("FBAPackWebPages", "InvalidEmail"); break; case MembershipCreateStatus.InvalidAnswer: lblAnswerMessage.Text = LocalizedString.GetGlobalString("FBAPackWebPages", "InvalidAnswer"); break; case MembershipCreateStatus.InvalidQuestion: lblQuestionMessage.Text = LocalizedString.GetGlobalString("FBAPackWebPages", "InvalidQuestion"); break; case MembershipCreateStatus.InvalidUserName: lblMessage.Text = LocalizedString.GetGlobalString("FBAPackWebPages", "InvalidUserName"); break; case MembershipCreateStatus.ProviderError: lblMessage.Text = LocalizedString.GetGlobalString("FBAPackWebPages", "ProviderError"); break; case MembershipCreateStatus.UserRejected: lblMessage.Text = LocalizedString.GetGlobalString("FBAPackWebPages", "UserRejected"); break; default: lblMessage.Text = LocalizedString.GetGlobalString("FBAPackWebPages", "UnknownError"); break; } }
protected override void OnInit(System.EventArgs e) { // display error if membership provider not configured if (!Utils.IsProviderConfigured()) { lblMessage.Text = LocalizedString.GetGlobalString("FBAPackWebPages", "MembershipNotConfigured"); MemberGrid.Visible = false; ToolBarPlaceHolder.Visible = false; onetidNavNodesTB.Visible = false; SearchControls.Visible = false; } base.OnInit(e); }
protected override void OnLoad(EventArgs e) { this.CheckRights(); // display error confirmation message string userName = Request.QueryString["USERNAME"]; if (!string.IsNullOrEmpty(userName)) { deleteMsg.Text = string.Format(localizedMsg.Text, userName); } else { SPUtility.TransferToErrorPage(LocalizedString.GetGlobalString("FBAPackWebPages", "UserNotFound")); } }
protected override void OnInit(System.EventArgs e) { // display error if membership provider not configured if (!Utils.IsProviderConfigured()) { lblMessage.Text = LocalizedString.GetGlobalString("FBAPackWebPages", "MembershipNotConfigured"); RoleGrid.Visible = false; ToolBarPlaceHolder.Visible = false; onetidNavNodesTB.Visible = false; } // ModifiedBySolvion // bhi - 19.12.2011 // Show status when roles a not enabled else { MembershipSettings settings = new MembershipSettings(SPContext.Current.Web); if (!settings.EnableRoles) { string startupScriptName = "RolesNotEnabledInfo"; if (!Page.ClientScript.IsStartupScriptRegistered(startupScriptName)) { StringBuilder script = new StringBuilder(); script.AppendLine("ExecuteOrDelayUntilScriptLoaded(showRoleStatus, 'SP.js')"); script.AppendLine("function showRoleStatus() {"); script.AppendLine("var roleStatusID = SP.UI.Status.addStatus('Information : ', 'Roles are not enabled. You can enable roles in the <a href=\"/_layouts/15/FBA/Management/FBASiteConfiguration.aspx\">FBA Site Configuration</a>.', true);"); script.AppendLine("SP.UI.Status.setStatusPriColor(roleStatusID, \"yellow\");"); script.AppendLine("}"); Page.ClientScript.RegisterStartupScript(this.GetType(), startupScriptName, script.ToString(), true); } } } // EndModifiedBySolvion base.OnInit(e); }
protected override void OnLoad(EventArgs e) { this.CheckRights(); // init _showRoles = (new MembershipSettings(SPContext.Current.Web)).EnableRoles; // get user info string userName = this.Request.QueryString["USERNAME"]; SPUser spuser = null; try { spuser = this.Web.AllUsers[Utils.EncodeUsername(userName)]; } catch { } MembershipUser user = Utils.BaseMembershipProvider().GetUser(userName, false); if (user != null) { if (!Page.IsPostBack) { // load user props if (spuser != null) { SPSite site = SPContext.Current.Site; SPWeb web = site.RootWeb; txtUsername.Text = spuser.Email; txtFullName.Text = spuser.Name; SPListItem userItem = web.SiteUserInfoList.GetItemById(spuser.ID); txtCMITLocation.Text = userItem["CMIT Location"] as string == null ? string.Empty : userItem["CMIT Location"] as string; txtTelephoneNumber.Text = userItem["Telephone Number"] as string == null ? string.Empty : userItem["Telephone Number"] as string; txtTitle.Text = userItem["CMITTitle"] as string == null ? string.Empty : userItem["CMITTitle"] as string; txtDatofProvisionaing.SelectedDate = (userItem["Date of provisioning"] as DateTime?).HasValue ? (userItem["Date of provisioning"] as DateTime?).Value : user.CreationDate; } else { txtUsername.Text = user.Email; txtFullName.Text = user.UserName; } txtUsername.Text = user.UserName; isActive.Checked = user.IsApproved; isLocked.Checked = user.IsLockedOut; isLocked.Enabled = user.IsLockedOut; // if roles activated display roles if (_showRoles) { RolesSection.Visible = true; GroupSection.Visible = false; try { // load roles string[] roles = Utils.BaseRoleProvider().GetAllRoles(); rolesList.DataSource = roles; rolesList.DataBind(); // select roles associated with the user for (int i = 0; i < roles.Length; i++) { ListItem item = rolesList.Items.FindByText(roles[i].ToString()); if (item != null) { item.Selected = Utils.BaseRoleProvider().IsUserInRole(user.UserName, roles[i].ToString()); } } } catch (Exception ex) { Utils.LogError(ex, true); } } // otherwise display groups else { GroupSection.Visible = true; RolesSection.Visible = false; try { // load groups groupList.DataSource = this.Web.SiteGroups; groupList.DataBind(); if (spuser != null) { // select groups associated with the user foreach (SPGroup group in spuser.Groups) { ListItem item = groupList.Items.FindByText(group.Name); if (item != null) { item.Selected = true; } } } } catch (Exception ex) { Utils.LogError(ex, true); } } } } else { SPUtility.TransferToErrorPage(LocalizedString.GetGlobalString("FBAPackWebPages", "UserNotFound")); } }
protected void OnSubmit(object sender, EventArgs e) { // get user info string userName = this.Request.QueryString["USERNAME"]; SPUser spuser = null; // This could be done with EnsureUsers, which won't throw an exception if the user hasn't logged on to the site. try { spuser = this.Web.AllUsers[Utils.EncodeUsername(userName)]; } catch { } MembershipUser user = Utils.BaseMembershipProvider().GetUser(userName, false); // check user exists if (user != null) { try { // TODO: If we want the Email to be used for the user account, we need to delete the user and create a new one with the new email address. // This will mean we need to iterate over the groups that the user is a member of, in all site collections in all web apps, and add the new user // to those groups. In the meantime, we allow the email to be changed, but this won't update the account username. // update membership provider info user.Email = txtUsername.Text; user.IsApproved = isActive.Checked; //Unlock Account if (user.IsLockedOut && !isLocked.Checked) { user.UnlockUser(); } try { Utils.BaseMembershipProvider().UpdateUser(user); } catch (System.Configuration.Provider.ProviderException ex) { lblMessage.Text = ex.Message; return; } // if roles enabled add/remove user to selected role(s) if (_showRoles) { for (int i = 0; i < rolesList.Items.Count; i++) { if (rolesList.Items[i].Selected) { if (!Utils.BaseRoleProvider().IsUserInRole(user.UserName, rolesList.Items[i].Value)) { Utils.BaseRoleProvider().AddUsersToRoles(new string[] { user.UserName }, new string[] { rolesList.Items[i].Value }); } } else { if (Utils.BaseRoleProvider().IsUserInRole(user.UserName, rolesList.Items[i].Value)) { Utils.BaseRoleProvider().RemoveUsersFromRoles(new string[] { user.UserName }, new string[] { rolesList.Items[i].Value }); } } } } // or add/remove user to selected group(s) else { for (int i = 0; i < groupList.Items.Count; i++) { string groupName = groupList.Items[i].Value; // determine whether user is in group bool userInGroup = false; if (spuser != null) { foreach (SPGroup group in spuser.Groups) { if (group.Name == groupName) { userInGroup = true; break; } } } // if selected add user to group if (groupList.Items[i].Selected) { // only add if not already in group if (!userInGroup) { //Add the user to SharePoint if they're not already a SharePoint user if (spuser == null) { try { spuser = this.Web.EnsureUser(Utils.EncodeUsername(userName)); } catch (Exception ex) { lblMessage.Text = LocalizedString.GetGlobalString("FBAPackWebPages", "ErrorAddingToSharePoint"); Utils.LogError(ex, false); return; } } this.Web.SiteGroups[groupName].AddUser(spuser); } } // else remove user from group else { // only attempt remove if actually in the group if (userInGroup) { this.Web.SiteGroups[groupName].RemoveUser(spuser); } } } } SPSite site = SPContext.Current.Site; SPWeb web = site.RootWeb; SPList list = web.SiteUserInfoList; SPListItem userItem = null; // update sharepoint user info if (spuser != null) { spuser.Email = txtUsername.Text; spuser.Name = txtFullName.Text; spuser.Update(); try { userItem = list.GetItemById(spuser.ID); } catch (Exception ex) { Utils.LogError(ex); } if (userItem != null) { userItem["CMIT Location"] = txtCMITLocation.Text; userItem["CMITTitle"] = txtTitle.Text; userItem["Telephone Number"] = txtTelephoneNumber.Text; userItem["Date of provisioning"] = txtDatofProvisionaing.SelectedDate; userItem.Update(); } } SPUtility.Redirect("FBA/Management/UsersDisp.aspx", SPRedirectFlags.RelativeToLayoutsPage | SPRedirectFlags.UseSource, this.Context); } catch (Exception ex) { Utils.LogError(ex, true); } } else { SPUtility.TransferToErrorPage(LocalizedString.GetGlobalString("FBAPackWebPages", "UserNotFound")); } }
protected void OnSubmit(object sender, EventArgs e) { // ModifiedBySolvion // bhi - 09.01.2012 // Reset message labels lblMessage.Text = lblAnswerMessage.Text = lblEmailMessage.Text = lblPasswordMessage.Text = lblQuestionMessage.Text = ""; // EndModifiedBySolvion bool _showRoles = (new MembershipSettings(SPContext.Current.Web)).EnableRoles; // check to see if username already in use MembershipUser user = Utils.BaseMembershipProvider().GetUser(txtUsername.Text, false); if (user == null) { try { // get site reference string provider = Utils.GetMembershipProvider(this.Site); // create FBA database user MembershipCreateStatus createStatus; if (Utils.BaseMembershipProvider().RequiresQuestionAndAnswer) { user = Utils.BaseMembershipProvider().CreateUser(txtUsername.Text, txtPassword.Text, txtEmail.Text, txtQuestion.Text, txtAnswer.Text, isActive.Checked, null, out createStatus); } else { user = Utils.BaseMembershipProvider().CreateUser(txtUsername.Text, txtPassword.Text, txtEmail.Text, null, null, isActive.Checked, null, out createStatus); } if (createStatus != MembershipCreateStatus.Success) { SetErrorMessage(createStatus); return; } if (user == null) { lblMessage.Text = LocalizedString.GetGlobalString("FBAPackWebPages", "UnknownError"); return; } bool groupAdded = false; if (_showRoles) { for (int i = 0; i < rolesList.Items.Count; i++) { if (rolesList.Items[i].Selected) { Utils.BaseRoleProvider().AddUsersToRoles(new string[] { user.UserName }, new string[] { rolesList.Items[i].Value }); } } // add user to SharePoint whether a role was selected or not AddUserToSite(Utils.EncodeUsername(user.UserName), user.Email, txtFullName.Text); } else { // add user to each group that was selected for (int i = 0; i < groupList.Items.Count; i++) { if (groupList.Items[i].Selected) { // add user to group SPGroup group = this.Web.SiteGroups[groupList.Items[i].Value]; group.AddUser( Utils.EncodeUsername(user.UserName), user.Email, txtFullName.Text, ""); // update group.Update(); groupAdded = true; } } // if no group selected, add to site with no permissions if (!groupAdded) { AddUserToSite(Utils.EncodeUsername(user.UserName), user.Email, txtFullName.Text); } } // Email User if ((emailUser.Checked == true)) { //InputFormTextBox txtEmailSubject = (InputFormTextBox)emailUser.FindControl("txtEmailSubject"); //InputFormTextBox txtEmailBody = (InputFormTextBox)emailUser.FindControl("txtEmailBody"); if ((!string.IsNullOrEmpty(txtEmailSubject.Text)) && (!string.IsNullOrEmpty(txtEmailBody.Text))) { Email.SendEmail(this.Web, user.Email, txtEmailSubject.Text, txtEmailBody.Text); } } SPUtility.Redirect("FBA/Management/UsersDisp.aspx", SPRedirectFlags.RelativeToLayoutsPage | SPRedirectFlags.UseSource, this.Context); } catch (Exception ex) { Utils.LogError(ex, true); } } else { lblMessage.Text = LocalizedString.GetGlobalString("FBAPackWebPages", "DuplicateUserName");; } }
protected void OnSubmit(object sender, EventArgs e) { // ModifiedBySolvion // bhi - 09.01.2012 // Reset message labels //lblMessage.Text = lblAnswerMessage.Text = lblEmailMessage.Text = lblPasswordMessage.Text = lblQuestionMessage.Text = ""; // EndModifiedBySolvion bool _showRoles = (new MembershipSettings(SPContext.Current.Web)).EnableRoles; // check to see if username already in use MembershipUser user = Utils.BaseMembershipProvider().GetUser(txtEmail.Text, false); if (user == null) { try { // get site reference string provider = Utils.GetMembershipProvider(this.Site); // create FBA database user MembershipCreateStatus createStatus; if (Utils.BaseMembershipProvider().RequiresQuestionAndAnswer) { user = Utils.BaseMembershipProvider().CreateUser(txtEmail.Text, txtPassword.Text, txtEmail.Text, txtQuestion.Text, txtAnswer.Text, isActive.Checked, null, out createStatus); } else { user = Utils.BaseMembershipProvider().CreateUser(txtEmail.Text, txtPassword.Text, txtEmail.Text, null, null, isActive.Checked, null, out createStatus); } if (createStatus != MembershipCreateStatus.Success) { SetErrorMessage(createStatus); return; } if (user == null) { lblEmailMessage.Text = LocalizedString.GetGlobalString("FBAPackWebPages", "UnknownError"); return; } bool groupAdded = false; if (_showRoles) { for (int i = 0; i < rolesList.Items.Count; i++) { if (rolesList.Items[i].Selected) { Utils.BaseRoleProvider().AddUsersToRoles(new string[] { user.UserName }, new string[] { rolesList.Items[i].Value }); } } // add user to SharePoint whether a role was selected or not AddUserToSite(Utils.EncodeUsername(user.UserName), user.Email, txtFullName.Text); } else { // add user to each group that was selected for (int i = 0; i < groupList.Items.Count; i++) { if (groupList.Items[i].Selected) { // add user to group SPGroup group = this.Web.SiteGroups[groupList.Items[i].Value]; group.AddUser( Utils.EncodeUsername(user.UserName), user.Email, txtFullName.Text, ""); // update group.Update(); groupAdded = true; } } // if no group selected, add to site with no permissions if (!groupAdded) { AddUserToSite(Utils.EncodeUsername(user.UserName), user.Email, txtFullName.Text); } } SPSite site = SPContext.Current.Site; SPWeb web = site.RootWeb; SPList list = web.SiteUserInfoList; SPUser SPuser = web.AllUsers.GetByEmail(txtEmail.Text); SPListItem userItem = null; try { userItem = list.GetItemById(SPuser.ID); } catch (Exception ex) { Utils.LogError(ex); } if (userItem != null) { userItem["CMIT Location"] = txtCMITLocation.Text; userItem["CMITTitle"] = txtTitle.Text; userItem["Telephone Number"] = txtTelephoneNumber.Text; userItem["Date of provisioning"] = txtDatofProvisionaing.SelectedDate; userItem.Update(); } // Email User if ((emailUser.Checked == true)) { if ((!string.IsNullOrEmpty(txtEmailSubject.Text)) && (!string.IsNullOrEmpty(txtEmailBody.Text))) { var emailBody = txtEmailBody.Text + Environment.NewLine + "Use your email as user name :" + txtEmail.Text + " and password as :" + txtPassword.Text + Environment.NewLine + "We recommand you to change your password when you login for first time."; Email.SendEmail(this.Web, user.Email, txtEmailSubject.Text, emailBody); } } SPUtility.Redirect("FBA/Management/UsersDisp.aspx", SPRedirectFlags.RelativeToLayoutsPage | SPRedirectFlags.UseSource, this.Context); } catch (Exception ex) { Utils.LogError(ex, true); } } else { lblEmailMessage.Text = LocalizedString.GetGlobalString("FBAPackWebPages", "DuplicateUserName");; } }