ASP.NET方式在工作组内修改帐户密码

背景:
以工作组的方式在服务器A上架设了TFS2008,分配给组员的帐户是服务器A的本地帐户,在这种情况下,组员如果想要修改密码,就需要提供一个修改的平台。

 

首先想到的是,在TFS的Sharepoint门户上集成修改密码的页面,但是很遗憾,在网上找到的Web part控件都有些问题,实在不行了,就只好自己单独弄了一个ASP.NET页面,其实还是使用DirectoryEntry来使用修改。修改密码的命令有"ChangePassword"和"SetPassword",这两个命令的区别在于:


"ChangePassword"需要旧密码,它对于修改密码的帐户没有要求。
"SetPassword"不需要旧密码,但它要求被修改的用户拥有管理员权限。

 

最后选择使用ChangePassword,码如下: 

Default.aspx.cs
/*
* Created by Aries 
http://www.cnblogs.com/sansi
*/
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Web;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.DirectoryServices;

namespace  ChangePwd
{
    
public   partial   class  _Default : System.Web.UI.Page
    {
        
protected   void  Page_Load( object  sender, EventArgs e)
        {
            lableUserName.Text 
=  Environment.UserName;
        }

        
protected   void  btnChangePwd_Click( object  sender, EventArgs e)
        {
            
string  oldPassword  =  textOldPwd.Text.Trim();
            
string  newPassword  =  textNewPwd.Text.Trim();
            
string  newPassword2  =  textNewPwd2.Text.Trim();

            
if  (oldPassword  ==  newPassword)
            {
                lableMSG.Text 
=   " 新密码与旧密码不能相同 " ;
                
return ;
            }
            lableMSG.Text = ChangePassword(Environment.UserName, oldPassword, newPassword);
        }

        
private   string  ChangePassword( string  username,  string  oldpass,  string  newpass)
        {
            
string  machineAndUser  =  Environment.MachineName  +   " \\ "   +  username;
            
string  adsPath  =  String.Format( " WinNT://{0}/{1}, user " , Environment.MachineName, username);
            DirectoryEntry user 
=   new  DirectoryEntry(adsPath, machineAndUser, oldpass, AuthenticationTypes.Secure);
            
try
            {
                user.Invoke(
" ChangePassword " new   object [] { oldpass, newpass });
                
return   string .Format( " 更改密码成功 " );
            }
            
catch  (Exception ex)
            {
                
string  message  =  ex.Message;
                
while  ((ex  =  ex.InnerException)  !=   null )
                {
                    message 
+=   " ::  "   +  ex.Message;
                }
                
return  message;
            }
            
finally
            {
                user.Dispose();
            }
        }
    }
}

 

Default.aspx
<% @ Page Language = " C# "  AutoEventWireup = " true "  CodeBehind = " Default.aspx.cs "  Inherits = " ChangePwd._Default "   %>

<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >

< html  xmlns ="http://www.w3.org/1999/xhtml"   >
< head  runat ="server" >
    
< title > 修改密码 </ title >
    
< style  type ="text/css" >
        body
{  font-family : Tahoma ;  font-size : 14px ; }
        .passwordbox
{  width : 600px ;  text-align : left ; }
        .passwordbox .text
{  width : 120px ;  height : 16px ;  border : 1px solid #ccc ; }
        .passwordbox .title
{  color : Blue ; }
        .passwordbox p
{  text-indent : 20px ; }
        .red
{  color : Red ; }
    
</ style >
</ head >
< body >
    
< form  id ="form1"  runat ="server" >
    
< div  class ="passwordbox" >
        
< class ="title" >
            
< b >< asp:Label  ID ="lableUserName"  Text =""  runat ="server" ></ asp:Label ></ b > ,您可以在这里修改帐户密码
        
</ p >
        
< p >
            
< lable >   旧密码: </ lable >< asp:TextBox  CssClass ="text"  ID ="textOldPwd"  Text =""  TextMode ="Password"  runat ="server" ></ asp:TextBox >
            
< asp:RequiredFieldValidator  ID ="RequiredFieldValidator1"  runat ="server"  ErrorMessage ="RequiredFieldValidator"  ControlToValidate ="textOldPwd"  Text ="*" ></ asp:RequiredFieldValidator >
        
</ p >
        
< p >
            
< lable >   新密码: </ lable >< asp:TextBox  CssClass ="text"  ID ="textNewPwd"  Text =""  TextMode ="Password"  runat ="server" ></ asp:TextBox >
            
< asp:RequiredFieldValidator  ID ="RequiredFieldValidator2"  runat ="server"  ErrorMessage ="RequiredFieldValidator"  ControlToValidate ="textNewPwd"  Text ="*" ></ asp:RequiredFieldValidator >             
        
</ p >
        
< p >
            
< lable > 确认新密码: </ lable >< asp:TextBox  CssClass ="text"  ID ="textNewPwd2"  Text =""  TextMode ="Password"  runat ="server" ></ asp:TextBox >
            
< asp:RequiredFieldValidator  ID ="RequiredFieldValidator3"  runat ="server"  ErrorMessage ="RequiredFieldValidator"  ControlToValidate ="textNewPwd2"  Text ="*" ></ asp:RequiredFieldValidator >
            
< asp:CompareValidator  ID ="CompareValidator1"  runat ="server"  ErrorMessage ="CompareValidator"  ControlToCompare ="textNewPwd"  ControlToValidate ="textNewPwd2"  Text ="两次新密码不相同" ></ asp:CompareValidator >
        
</ p >
        
< style =" text-align:left; padding-left:100px;" >< asp:Button  ID ="btnChangePwd"  runat ="server"  Width ="80px"  Text ="修改"  OnClick ="btnChangePwd_Click"   /></ p >
        
< p >< asp:Label  ID ="lableMSG"  CssClass ="red"  Text =""  runat ="server" ></ asp:Label ></ p >
    
</ div >
    
</ form >
</ body >
</ html >

你可能感兴趣的:(asp.net)