因为项目的要求,要做一个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

相关文章:

  • 2021-09-04
  • 2021-04-15
  • 2022-12-23
  • 2021-09-22
  • 2022-12-23
  • 2021-06-22
  • 2022-02-10
  • 2022-01-11
猜你喜欢
  • 2021-06-18
  • 2022-03-10
  • 2022-02-28
  • 2022-12-23
  • 2021-10-18
  • 2021-11-18
相关资源
相似解决方案