ASP.NET中关于AD密码修改问题

      最近在做关于AtiveDirectory(AD)方面的开发.由于对AD并不是十分的了解,在网上也没查到什么非常好的参考资料(当然也有一些),朋友同事也不是十分了解,呵呵,自己弄了好久。还好基本弄好了要做的功能。就修改密码这一个方面就弄了好久,经过多方面的资料参考搞定了。由于以前从没做过这方面的开发,所以用了很多的时间。当然要记录下来,方便以后参考。

      经过多方面的资料参考,在ASP.NET中修改AD中的密码最要有两种方法:一种是用System.Web.Security命名空间下的MembershipUser、Membership两类来实现AD密码修改;另一种用System.DirectoryServices命名空间下的DirectoryEntry 、DirectorySearcher 两类来实现AD密码修改。当然用哪种方法来修改AD密码都要对修改密码方法的类要了解清楚,在MSDN中有很详细的介绍,可以去看看,了解清楚方便运用。

      在修改密码是你所输入的新密码要AD密码强度的验证才行,也就是一般我们在注册用户时提示的密码复杂性。在AD中默认吗密码强度是:密码长度大于7,必须包含大写或小写字母(a-z或A-Z)、数字和至少1个特殊字符。当然你也可以修改AD的密码强度要求。修改AD的密码强度要求是在 管理工具》域安全策越 打开一个窗口“默认域安全策越”,在里面选择:安全设置》账户策略》密码策略。就可以看到AD的密码强度要求住这里可以修改密码强度,如果有不清楚的你可以看看每个设置都有详细的说明。

     修改密码是要获取到AD的密码强度要求,最好不要再验证的时候手动写上,要动态的获取密码强度要求。因为我们不可能知道那天就要修改AD的密码强度要求了,如果你写死了就很可能验证时通不过,就又要重新修改密码,这样很麻烦,所以最好动态的获取AD密码强度要求。如果你是用Membership验证的话,在Web.congfig中配置Membership时你就要配置AD的密码强度要求,你可以在这里获取AD的密码强度要求,但是这个里面的配置不是很全面。最好读取配置文件,在你配置好AD密码策略是,这些密码策略保存在一个配置文件(GptTmpl.inf)中,它的位置在\sysvol\域名\Policies\{31B2F340-016D-11D2-945F-00C04FB984F9}\MACHINE\Microsoft\Windows NT\SecEdit\GptTmpl.inf。读取这个配置文件下面有介绍挺简单的。

下面详细介绍一下两种方法:

方法一:用System.Web.Security命名空间下的MembershipUser、Membership两类来实现AD密码修改。

关键代码(此代码在ASP.NET MVC中编写):

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
1 public ActionResult UpdatePassword( string oldpassword, string newpassword)
2 {
3
4 string message = "" ;
5 string pwdComplexity = ReadValue( " System Access " , " PasswordComplexity " ); // 获取密码复杂性策略
6   string pwdLength = ReadValue( " System Access " , " MinimumPasswordLength " ); // 获取密码长度最小值
7   if (newpassword.Length < Int32.Parse(pwdLength)) // 判断密码的长度是否足够
8 {
9 message = " 密码长度必须大于等于 " + pwdLength;
10 return null ;
11 }
12 // 验证密码复杂性
13 switch (pwdComplexity)
14 {
15 case " 1 " :
16
17 break ;
18 case " 0 " :
19 break ;
20 }
21
22 MembershipUser u = Membership.GetUser(User.Identity.Name); // 根据用户名获取用户信息
23 try
24 {
25 if (u.ChangePassword(oldpassword, newpassword)) // 修改密码
26 {
27 message = " 修改成功! " ;
28
29 }
30 else
31 {
32 message = " 修改失败! " ;
33
34 }
35 }
36 catch (Exception)
37 {
38 throw ;
39 }
40 return View();
41 }
42
43 // 文件路径
44 string _filePath;
45 private string filePath
46 {
47 get
48 {
49 if ( string .IsNullOrEmpty(_filePath))
50 {
51 _filePath = " \\\\ " + " 192.168.1.2 " + " \\sysvol\\ " + " xinge.com "
52 + " \\Policies\\{31B2F340-016D-11D2-945F-00C04FB984F9}\\MACHINE\\Microsoft\\Windows NT\\SecEdit\\GptTmpl.inf " ;
53 }
54 return _filePath;
55 }
56 }
57 [DllImport( " kernel32 " )]
58 private static extern int GetPrivateProfileString( string section, string key, string def, StringBuilder retVal, int size, string filePath);
59 // 获取配置文件的信息
60 public string ReadValue( string strSection, string strKey)
61 {
62 if (filePath.Length == 0 )
63 {
64 throw new Exception( " 没有设置路径 " );
65 }
66 StringBuilder sb = new StringBuilder();
67 int i = GetPrivateProfileString(strSection, strKey, "" , sb, 255 , this .filePath);
68 return sb.ToString();
69 }
70
71

方法二:用System.DirectoryServices命名空间下的DirectoryEntry 、DirectorySearcher 两类来实现AD密码修改。

关键代码(此代码在ASP.NET MVC中编写):

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
1 public ActionResult UpdatePassword()
2 {
3 string a = " pass@word " ;
4 string oldPwd = "q@12345678 " ;
5 string newPwd = "q@123456 " ;
6 string username = " shishi " ;
7
8 DirectoryEntry de = new DirectoryEntry( " LDAP://192.168.1.2/OU=组织架构,DC=Xinge,DC=COM " , username, oldPwd);
9 DirectorySearcher ds = new DirectorySearcher(de);
10 ds.Filter = " (sAMAccountName= " + username + " ) " ; // 查询条件
11 ds.SearchScope = SearchScope.Subtree; // 查询多层
12 SearchResult sr = ds.FindOne(); // 查询出用户
13 DirectoryEntry usr = sr.GetDirectoryEntry();
14 usr.Invoke( " ChangePassword " , new object [] { oldPwd, newPwd }); // 调ChangePassword方法修改密码
15
16 objDE.CommitChanges(); // 提交修改
17
18

方法二中密码强度验证和方法一中的一样。

转载于:https://www.cnblogs.com/luojun2000/archive/2010/05/08/1730304.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值