SharePoint 修改密码Web Part部署方案
1. 以管理员身份打开Visual Studio2010,新建项目,模板选择SharePoint2010,平台选择.NET Framework 3.5, 新建空白SharePoint项目,编辑名称changePWD及位置,点击确定。
2. 弹出SharePoint 自定义向导。填写预调试站点,点击验证。验证成功后,选择“部署为场解决方案”,点击确定。
3. 右键点击changePWD,添加----新建项,选择可视Web部件passwordChange。
4. 在解决方案资源管理器中,右键单击引用,添加引用。在.NET选项卡选择System.DirectoryServices.AccountManagement。
5. 编辑代码。
passwordChange.cs:
using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
namespace changePWD.passwordChange
{
[ToolboxItemAttribute(false)]
public class passwordChange : WebPart
{
// 当更改可视 Web 部件项目项时,Visual Studio 可能会自动更新此路径。
private const string _ascxPath = @"~/_CONTROLTEMPLATES/changePWD/passwordChange/passwordChangeUserControl.ascx";
public string _domainName;
[Personalizable(PersonalizationScope.Shared)]
[WebBrowsable(true)]
[Category("自定义设置")]
[WebDisplayName("请输入域服务器的计算机全名。如smallville-pc")]
public string DomainName
{
get { return _domainName ?? (_domainName = "smallville-pc"); }//这里是域名
set { _domainName = value; }
}
protected override void CreateChildControls()
{
Control control = Page.LoadControl(_ascxPath);
Controls.Add(control);
}
}
}
passwordChangeUserControl.ascx :
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="passwordChangeUserControl.ascx.cs" Inherits="changePWD.passwordChange.passwordChangeUserControl" %>
更改密码注意事项:
1.密码必须包含以下三部分:字母,数字,特殊符号
2.密码长度必须大于6位
3.新密码不能与最近5次使用的密码相同
4.一天之内只允许修改一次密码。
5.密码修改后有5分钟的缓冲时间,5分钟后旧密码作废。
6.发生更改密码遗失,请联系系统管理员
7.联系邮箱ContactMe@126.com
用户名:
输入旧密码
输入新密码
再次输入新密码
passwordChangeUserControl.ascx.cs :
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.Security;
using System.DirectoryServices.AccountManagement;
using Microsoft.SharePoint;
namespace changePWD.passwordChange
{
public partial class passwordChangeUserControl : UserControl
{
#region 字符串
string domainName = string.Empty;
string logName = string.Empty;
string splited = string.Empty;
#endregion
#region methods
protected void Page_Load(object sender, EventArgs e)
{
domainName = ((passwordChange)Parent).DomainName;
logName = SPContext.Current.Web.CurrentUser.LoginName;
if (logName.IndexOf("\\") >= 0)
{
splited = logName.Split('\\')[1];
}
SplitedLabel.Text = splited;
}
#region 主要功能实现
protected void OKButton_Click(object sender, EventArgs e)
{
string oldpwd = OldCode.Text;
string newpwd = NewCode2.Text;
OKButton.Page.Validate();
if (OKButton.Page.IsValid)
{
try
{
PrincipalContext context = new PrincipalContext(ContextType.Domain, domainName, splited, oldpwd);
UserPrincipal currentPrincipal = UserPrincipal.FindByIdentity(context, splited);
SPSecurity.RunWithElevatedPrivileges(delegate() //提升权限执行
{
currentPrincipal.ChangePassword(oldpwd, newpwd);
currentPrincipal.Save();
});
StaLabel.Text = "密码修改成功";
}
catch (PasswordException pder)
{
StaLabel.Text = "PWD:[" + pder.Message + "]";
}
catch (Exception aner)
{
StaLabel.Text = "密码修改错误:[" + aner.Message + "]";
}
}
else
{
StaLabel.Text = "按提示更改所填信息";
}
}
#endregion
protected void ClearButton_Click(object sender, EventArgs e)
{
OldCode.Text = string.Empty;
NewCode1.Text = string.Empty;
NewCode2.Text = string.Empty;
}
#endregion
}
}
7. 在页面上添加此Web部件。