惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
美团技术团队
Recent Announcements
Recent Announcements
B
Blog
GbyAI
GbyAI
雷峰网
雷峰网
博客园_首页
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Tailwind CSS Blog
M
MIT News - Artificial intelligence
V
V2EX
人人都是产品经理
人人都是产品经理
爱范儿
爱范儿
L
LangChain Blog
Microsoft Security Blog
Microsoft Security Blog
宝玉的分享
宝玉的分享
A
About on SuperTechFans
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
U
Unit 42
Hugging Face - Blog
Hugging Face - Blog
F
Fortinet All Blogs
N
Netflix TechBlog - Medium
Last Week in AI
Last Week in AI
aimingoo的专栏
aimingoo的专栏

博客园 - db's jim

NuGet 无法连接到远程服务器-解决方法(转) 未能解析此远程名称:’nuget.org’ 解决方法(转) [转]jquery对事件冒泡的处理方法 Asp.net中的认证与授权 JS的事件监听机制 用映射的方法获取当前方法的名称 log4net的各种Appender配置示例(转) System.Diagnostics命名空间里的Debug类和Trace类的用途(收藏) Newtonsoft.Json处理日期问题 Failed to execute request because the App-Domain could not be created C#Windows服务程序安装 VS2010快捷键 生成方法存根 (Stub) NHibernate 事务查询的更新事件 Nhibernate.hbm2ddl.auto配置详解 NHibernate.Tool.hbm2ddl SchemaExport SQL2005 开窗函数 将EXCEL文档导入SQL server 2005错误 castle ActiveRecord 初始化
通过WEB方式修改windows帐号的秘密
db's jim · 2011-10-01 · via 博客园 - db's jim

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