通过WEB方式修改windows帐号的秘密

因为项目的要求,要做一个WEB service,通过传入用户名和新的密码,更新Windows帐号的密码,代码入下:

[WebMethod]
public bool ChangePassword(string userName,string password,string validCode)
{
string msg = "";

if (validCode != getOriginValidCode())
{
msg = "无效的BI确认码!";
throw new Exception(msg);
;
}

string adPwd = System.Configuration.ConfigurationManager.AppSettings["adPwd"];
string adUser = System.Configuration.ConfigurationManager.AppSettings["adUser"];


DirectoryEntry myDirectoryEntry;
myDirectoryEntry = new DirectoryEntry("WinNT://" + Environment.MachineName, adUser, adPwd);

DirectoryEntry u = myDirectoryEntry.Children.Find(userName);
if (null == u)
{
msg = "无效的用户";
throw new Exception(msg);
}

u.Invoke("setPassword", password);
u.CommitChanges();

if (msg != "")
{
throw new Exception(msg);
}

return true;
}

运行是发现有错误:

u.Invoke("setPassword", password);

一般性拒绝访问错误

我估计是权限的问题,在事件查看器里看了下,发现是以NT AUTHORITY\NETWORK SERVICE来执行操作的,导致权限不足,修改web.config文件,添加以下两行后解决:
<authentication mode="Windows"/>
<identity impersonate="true" userName="userName" password="password"/>

类型的技术文章:
http://www.cnblogs.com/Anper/archive/2009/03/24/1420405.html

posted @ 2011-10-01 19:08  db's jim  阅读(248)  评论(0编辑  收藏  举报