sharepoint2010修改域密码
具体代码:
/// <summary>
/// update pwd
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnUpdate_Click(object sender, ImageClickEventArgs e)
{
string strName = System.Web.HttpContext.Current.User.Identity.Name.ToString();
strName = strName.Substring(strName.LastIndexOf("\\") + 1);
string strNewPwd = tbNewPwd.Text;
string strConfirmPwd = tbConfirmPwd.Text;
if (strNewPwd != strConfirmPwd)
{
lblMsg.Text = "新密码与确认密码不一致!";
}
else if (strNewPwd.Trim().Length < 6)
{
lblMsg.Text = "新密码长度至少为6个字符!";
}
else
{
int iCount = 10;
if(Regex.IsMatch(strNewPwd, "[0-9]"))
{
iCount++;
}
if(Regex.IsMatch(strNewPwd, "[A-Z]"))
{
iCount++;
}
if(Regex.IsMatch(strNewPwd, "[a-z]"))
{
iCount++;
}
if (Regex.IsMatch(strNewPwd, "[^a-zA-Z0-9\u4E00-\u9FA5]"))
{
iCount++;
}
if (iCount >= 3)
{
lblMsg.Text = ChangeADUserPassword("***.cn", strName, tbOldPwd.Text, strNewPwd);
}
else
{
lblMsg.Text = "新密码不符合密码策略,至少包含大写字母、小写字母、数字、特殊字符中3类!";
}
}
}
/// <summary>
/// change ad pwd
/// </summary>
/// <param name="DomainName"></param>
/// <param name="UserName"></param>
/// <param name="oldPass"></param>
/// <param name="newPass"></param>
/// <returns></returns>
public string ChangeADUserPassword(string DomainName, string UserName, string oldPass, string newPass)
// 用法:ChangeADUserPassword("AD", "VIRUS", "12345", "23456")
{
try
{
string strLDAP = "LDAP://" + DomainName;
string fullLoginName = DomainName + "\\" + UserName;
using (DirectoryEntry objDE = new DirectoryEntry(strLDAP, DomainName + "\\" + UserName, oldPass))
{
DirectorySearcher deSearcher = new DirectorySearcher(objDE);
deSearcher.Filter = "(&(objectClass=user)(sAMAccountName=" + UserName + "))";
DirectoryEntry usr = deSearcher.FindOne().GetDirectoryEntry();
usr.Invoke("ChangePassword", new Object[2] { oldPass, newPass });
usr.CommitChanges();
}
return ("更改域用户密码,操作成功!");
}
catch (Exception ex)
{
return ("更改失败,错误信息:" + ex.Message);
}
}
/// update pwd
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnUpdate_Click(object sender, ImageClickEventArgs e)
{
string strName = System.Web.HttpContext.Current.User.Identity.Name.ToString();
strName = strName.Substring(strName.LastIndexOf("\\") + 1);
string strNewPwd = tbNewPwd.Text;
string strConfirmPwd = tbConfirmPwd.Text;
if (strNewPwd != strConfirmPwd)
{
lblMsg.Text = "新密码与确认密码不一致!";
}
else if (strNewPwd.Trim().Length < 6)
{
lblMsg.Text = "新密码长度至少为6个字符!";
}
else
{
int iCount = 10;
if(Regex.IsMatch(strNewPwd, "[0-9]"))
{
iCount++;
}
if(Regex.IsMatch(strNewPwd, "[A-Z]"))
{
iCount++;
}
if(Regex.IsMatch(strNewPwd, "[a-z]"))
{
iCount++;
}
if (Regex.IsMatch(strNewPwd, "[^a-zA-Z0-9\u4E00-\u9FA5]"))
{
iCount++;
}
if (iCount >= 3)
{
lblMsg.Text = ChangeADUserPassword("***.cn", strName, tbOldPwd.Text, strNewPwd);
}
else
{
lblMsg.Text = "新密码不符合密码策略,至少包含大写字母、小写字母、数字、特殊字符中3类!";
}
}
}
/// <summary>
/// change ad pwd
/// </summary>
/// <param name="DomainName"></param>
/// <param name="UserName"></param>
/// <param name="oldPass"></param>
/// <param name="newPass"></param>
/// <returns></returns>
public string ChangeADUserPassword(string DomainName, string UserName, string oldPass, string newPass)
// 用法:ChangeADUserPassword("AD", "VIRUS", "12345", "23456")
{
try
{
string strLDAP = "LDAP://" + DomainName;
string fullLoginName = DomainName + "\\" + UserName;
using (DirectoryEntry objDE = new DirectoryEntry(strLDAP, DomainName + "\\" + UserName, oldPass))
{
DirectorySearcher deSearcher = new DirectorySearcher(objDE);
deSearcher.Filter = "(&(objectClass=user)(sAMAccountName=" + UserName + "))";
DirectoryEntry usr = deSearcher.FindOne().GetDirectoryEntry();
usr.Invoke("ChangePassword", new Object[2] { oldPass, newPass });
usr.CommitChanges();
}
return ("更改域用户密码,操作成功!");
}
catch (Exception ex)
{
return ("更改失败,错误信息:" + ex.Message);
}
}
如果你要完成更酷的效果,参考如下:
来自博客园http://www.cnblogs.com/Fengger/archive/2012/06/12/2546935.html
在网上看了几篇有关SharePoint修改密码的文章,总觉得用户体验效果不好,就自己又写了一篇,三张效果图足以说明一切,咱们看效果图吧
图片已经欣赏过了,咱们来学习一下,如何在自己的项目上集成吧
第一步:创建一个空元素,并添加XML配制
之后打开创建的空元素,再里面添加如下XML
<Elements xmlns="http://schemas.microsoft.com/sharepoint/"> <CustomAction Id="{F93B1F84-1DBE-4C10-82E3-2CA47346359E}" Title="修改密码" Description="此处修改的是域里面的密码" Sequence="1000" Location="Microsoft.SharePoint.StandardMenu" GroupId="PersonalActions" ImageUrl="~sitecollection/_layouts/images/menulistsettings.gif"> <UrlAction Url="javascript:portal_openModalDialog();"/> </CustomAction> </Elements>
第二步:在V4母板页上添加如下JS代码,用来增加用户体验效果
function portal_openModalDialog() { var options = SP.UI.$create_DialogOptions(); options.width = 500; options.height = 250; options.url = "/_layouts/SharePointProject/UpdatePassword.aspx"; options.dialogReturnValueCallback = Function.createDelegate(null, portal_modalDialogClosedCallback); SP.UI.ModalDialog.showModalDialog(options); } //SP.UI.ModalDialog.commonModalDialogClose(SP.UI.DialogResult.OK, 1); //关闭函数 function portal_modalDialogClosedCallback(result, value) { if (value == "1") { SP.UI.Notify.addNotification("修改成功"); } else if(value == "0"){ SP.UI.Notify.addNotification("修改失败,请重新修改"); } // if (result === SP.UI.DialogResult.OK) { // alert("点击了确定!"); // } // if (result === SP.UI.DialogResult.cancel) { // alert("点击了取消!"); // } } function closeDialog() { SP.UI.ModalDialog.commonModalDialogClose(SP.UI.DialogResult.cancel, 3); }
添加之后记得发布哦,否则会有意想不到的效果,自己尝试一下吧,我就不多说会有什么后果了
第三步:创建一个ASPX页面,代码如下
<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server"> <table cellpadding="0" cellspacing="0" border="0"> <tr> <td>旧密码:</td> <td><asp:TextBox ID="txtOldPwd" runat="server"></asp:TextBox></td> </tr> <tr> <td>新密码:</td> <td><asp:TextBox ID="txtNewPwd" runat="server"></asp:TextBox></td> </tr> <tr> <td>确认密码:</td> <td><asp:TextBox ID="txtConfirmPwd" runat="server"></asp:TextBox></td> </tr> <tr> <td><asp:Button ID="btnUpdate" runat="server" Text="保存" OnClick="btnUpdate_Click" /></td> <td><asp:Button ID="btnCancel" runat="server" Text="修改" OnClientClick="closeDialog()" /></td> </tr> </table> </asp:Content> <asp:Content ID="PageTitle" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server"> 修改密码 </asp:Content>
第四步:在后台cs文件里面写自己的业务处理
private string _userName; private string _domainName; private PrincipalContext _principalContext; private UserPrincipal _userPrincipal; protected void btnUpdate_Click(object sender, EventArgs e) { _userName = SPContext.Current.Web.CurrentUser.LoginName; if (_userName.IndexOf("\\") > 0) { try { _domainName = _userName.Split('\\')[0]; _userName = _userName.Split('\\')[1]; _principalContext = new PrincipalContext(ContextType.Domain, _domainName, _userName, txtOldPwd.Text); //这个方法也容易出问题,如果旧密码输入错误,这查找也会出错, _userPrincipal = UserPrincipal.FindByIdentity(_principalContext, _userName); if (_userPrincipal != null) { //这一点容易出错,是有关密码策略的问题 //密码不满足密码策略的要求。检查最小密码长度、密码复杂性和密码历史的要求。 (异常来自 HRESULT:0x800708C5) //_userPrincipal.ChangePassword(txtOldPwd.Text, txtNewPwd.Text); //_userPrincipal.Save(); Response.Write( "<script type=\"text/javascript\">window.frameElement.commonModalDialogClose(1, 1);</script>"); } } catch (Exception ex) { Response.Write( "<script type=\"text/javascript\">alert('"+ex.Message+"')</script>"); } } }
上面的代码要引用 System.DirectoryServices.AccountManagement.dll 这个文件,经常会出错的信息就是 密码不满足密码策略的要求。检查最小密码长度、密码复杂性和密码历史的要求。 (异常来自 HRESULT:0x800708C5) ,是因为域设置的策略问题,可以让管理员取消掉。