您的位置:首页 > 其它

SharePoint 2013 修改AD密码及一系列问题的解决方法

2016-04-11 10:33 711 查看

SharePoint 2013 修改AD密码的完全方案

 首先分析步骤如下三步;

修改密码显示位置。
修改密码的界面
修改密码代码。
修改AD的代码

如果出现如下问题,可能是你的域写错了,这里呀完整的域名称,例如dct.com



如果出现如下问题,是AD控制提示的约束错误。
1,可能是密码约束策略问题,例如强度不够。
2,可能是你的AD策略问题,有的AD限制,一个用户一天只能修改一下密码。
3,错误名称“ 出现一个约束冲突。(异常来自HRESULT:0x8007202F)”
修改3方法,http://forums.manageengine.com/topic/error-8007202f 下图是为了避免域名而引起的错误,自动填充了,域名。



ok 那我开始介绍代码,分为4部分介绍,

[align=left]首先介绍 单击系统帐号下,出现的修改用户密码[/align]



[align=left]再介绍webpart 出现的,界面图2[/align]

[align=left]HTML代码[/align]

<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">

<div>
<h3>
<span>修改密码</span>
</h3>
<table width="400px">
<tr>
<td>
域名全称:(已填充)
</td>
<td>
</td>
<td>
<asp:TextBox ID="txtdomain" runat="server"  >proj.cgdc.com.cn</asp:TextBox>
</td>
</tr>
<tr>
<td>
旧密码 :
</td>
<td>
</td>
<td>
<asp:TextBox ID="txtOld" runat="server" TextMode="Password"></asp:TextBox>
</td>
</tr>
<tr>
<td>
新密码 :
</td>
<td>
</td>
<td>
<asp:TextBox ID="txtPass1" runat="server" TextMode="Password"></asp:TextBox>
</td>
</tr>
<tr>
<td>
确认新密码:
<br />

</td>
<td>
</td>
<td>
<asp:TextBox ID="txtPass2" runat="server" TextMode="Password"></asp:TextBox>
</td>
</tr>

<tr>
<td colspan="3" align="center">
<asp:Literal ID="ltMsg" EnableViewState="false" runat="server" ></asp:Literal>
<br />
<asp:Button ID="btnChangePwd" runat="server" Text="修改密码" OnClick="btnChangePwd_Click" />
</td>
</tr>
</table>
<br />
<br />
</div>
</asp:Content>

<asp:Content ID="PageTitle" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server">
修改密码
</asp:Content>

<asp:Content ID="PageTitleInTitleArea" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server" >
修改密码
</asp:Content>
 

 

[align=left]接着CS端修改代码修改代码[/align]

using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Security.Principal;
using System.DirectoryServices.AccountManagement;
using System.DirectoryServices;

namespace SharePointProjectDemo.Layouts.ChangePassword
{
public class Impersonator
{
// Fields

private WindowsImpersonationContext ctx = null;

// Methods
public void BeginImpersonation()
{
try
{
if (!WindowsIdentity.GetCurrent().IsSystem)
{
this.ctx = WindowsIdentity.Impersonate(WindowsIdentity.GetCurrent().Token);
this.IsImpersonated = true;
}
}
catch
{
this.IsImpersonated = false;
}
}

public void StopImpersonation()
{
if (this.ctx != null)
{
this.ctx.Undo();
}
}

// Properties
public bool IsImpersonated
{
set;
get;
}
}


 
public partial class ChangePassword : LayoutsPageBase
{
protected void btnChangePwd_Click(object sender, EventArgs e)
{
txtdomain.Visible = true;
txtdomain.Enabled = false;
string str = this.txtPass1.Text.Trim();
string str2 = this.txtPass2.Text.Trim();
string str3 = this.txtOld.Text.Trim();
string str4 = this.txtdomain.Text.Trim();
if (string.IsNullOrWhiteSpace(str4))
{
this.ltMsg.Text = "域不能为空!";
}
else if (string.IsNullOrWhiteSpace(str3))
{
this.ltMsg.Text = "旧密码不能为空!";
}
else if (string.IsNullOrWhiteSpace(str))
{
this.ltMsg.Text = "新密码不能为空!";
}
else if (str == str2)
{
this.ChangeUserPassword(this.txtPass2.Text.Trim(), str3, str4);
}
else
{
this.ltMsg.Text = "两次新密码不一致,请检查!";
}
}

private void ChangeUserPassword(string NewPwd, string OldPwd, string domain)
{
try
{
Impersonator impersonator = new Impersonator();
impersonator.BeginImpersonation();
using (PrincipalContext context = this.GetPContext(OldPwd, domain))
{
using (UserPrincipal principal = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, GetLoginName()))
{
principal.ChangePassword(OldPwd, NewPwd);
}
}
if (impersonator.IsImpersonated)
{
impersonator.StopImpersonation();
this.ltMsg.Text = "已成功修改密码!";
}
else
{
this.ltMsg.Text = "无法修改您的密码,请联系您的系统管理员!";
}
}
catch (Exception exception)
{
this.ltMsg.Text = exception.Message;
}
}

private string GetDomainContainter(string domain)
{
string str = string.Empty;
string[] strArray = domain.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string str2 in strArray)
{
str = str + "DC=" + str2 + ",";
}
if (str.Length > 0)
{
str = str.Substring(0, str.Length - 1);
}
return str;
}

private string GetLoginName()
{

string username= SPContext.Current.Web.CurrentUser.LoginName.Replace("i:0#.w|", "");
if(username.EndsWith(@"\system"))
{
username = username.Replace("system", "spadmin");
//2016 add textbox for other user change the PWD. by chuantao.duan
}
return username;
}

private string GetLoginNameDomain()
{
string[] strArray = GetLoginName().Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries);
if (strArray.Length == 2)
{
return strArray[0];
}
return null;
}

private PrincipalContext GetPContext(string OldPwd, string domain)
{
return new PrincipalContext(ContextType.Domain, domain, this.GetDomainContainter(domain), ContextOptions.Negotiate, this.GetLoginName(), OldPwd);
}

protected void Page_Load(object sender, EventArgs e)
{
this.ltMsg.Text = GetLoginName().Replace("i:0#.w|", "");
//txtdomain.Text = "proj.cgdc.com.cn";
}
private  DirectoryEntry GetDomainRoot(string Domain)
{
// 2004-07-28, Leo Duran, Fix for IIS not being run on the AD Computer

return new DirectoryEntry(Domain, ADUserName, ADUserPassword);

}
}


 

[align=left] [/align]


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: