选择显示字体大小

为serv-u提供在线修改密码功能(2)

     用户密码的加密方法可以在ser-u官方网站的知识库查到
   http://rhinosoft.com/kbarticle.asp?refno=1177&prod=su
  manually entering encrypted passwords into the servudaemon.ini file
  to generate an encrypted password, first two random characters (the 'salt' - in the range a..z, a..z) are added to the beginning of the clear-text password. this is then hashed using md5 and the resulting hash is hex-encoded. the result of this is written as plain-text starting with the 2 salt characters followed by the hex-encoded hash.
  
  for a user account in the .ini file, this will look like:
  
  password=cb644fb1f31184f8d3d169b54b3d46ab1a
  
  the salt is the string "cb", the md5 hash is "644fb1f31184f8d3d169b54b3d46ab1a".
  
  when verifying a user's password, serv-u will do the same. it parses the salt from the user's stored password (ie. "cb" in this case), prepends it the password the user sent to it by the client, md5 hashes it, and compares the result with the stored hash. if the values are equal, then the entered password is correct.
  
  
  加密的方法也就是随机生成两个字母,然后将字母和密码进行拼接,再求它们的md5值,最后将随机字母放在md5值的前面便是加密后的密码。
   接下来就可以根据以上的分析编写程序来实现在线修改了。
   1 /**//// <summary>
   2 /// 获取指定字符串的md5值
   3 /// </summary>
   4 /// <param name="strcontent"></param>
   5 /// <returns></returns>
   6 public string md5( string strcontent )
   7 {
   8 system.security.cryptography.md5 md5 = new system.security.cryptography.md5cryptoserviceprovider();
   9 byte[] bytes = system.text.encoding.utf8.getbytes( strcontent );
   10 bytes = md5.computehash( bytes );
   11 md5.clear();
   12 string ret = "";
   13 for(int i=0 ; i<bytes.length ; i++)
   14 {
   15 ret += convert.tostring(bytes[i],16).padleft(2,'0');
   16 }
   17 return ret.padleft(32,'0').toupper();
   18 }
   19
   20
   21 /**//// <summary>
   22 /// 生成随便字符串,字符串长度为2
   23 /// </summary>
   24 /// <returns></returns>
   25 public string getrandomstring()
   26 {
   27 string strreturn = "";
   28 random ran = new random();
   29 strreturn += convert.tochar( ran.next( 26 ) + 'a' ).tostring();
   30 strreturn += convert.tochar( ran.next( 26 ) + 'a' ).tostring();
   31 return strreturn;
   32 }
   33
   34 //由指定的随机字母和登录密码生成加密后的密码
   35 public string createcrypassword( string strfrontchars, string strpassword )
   36 {
   37 return strfrontchars + md5( strfrontchars + strpassword ).toupper().trim();
   38 }
   39
   40 /**//// <summary>
   41 /// “修改密码”的点击事件,在此事件中对密码进行修改
   42 /// </summary>
   43 /// <param name="sender"></param>
   44 /// <param name="e"></param>
   45 private void btnmodifypwd_click(object sender, system.eventargs e)
   46 {
   47 string struserid = txtloginid.text;
   48 if( struserid == string.empty )
   49 {
   50 controlmessage.innerhtml = "用户名不能为空";
   51 return;
   52 }
   53
   54 //判断两次密码输入是否相同
   55 if( txtnewpassword.text != txtconfirmpassword.text )
   56 {
   57 controlmessage.innerhtml = "两次输入的密码不一致,请重新输入";
   58 return;
   59 }
   60
   61 inifile ini = new inifile( _strservudaemonpath );
   62 string strsectionvalue = "user=" + struserid.trim() + "1";
   63
   64 //通过读取指定用户的homedir来确定是否存在该用户
   65 if( ini.readstring( strsectionvalue, "homedir", "" ) == "" )
   66 {
   67 controlmessage.innerhtml = "指定的用户不存在";
   68 return;
   69 }
   70
   71 //开始判断密码是否正确
   72 string strpassword = ini.readstring( strsectionvalue, "password", "" );
   73
   74 string strpasswordfronttwochars;
   75 bool bpasswordright = false;
   76 if( strpassword.length > 2 )
   77 {
   78 //读取密码中包含的随机字母
   79 strpasswordfronttwochars = strpassword.substring( 0, 2 );
   80 if( createcrypassword( strpasswordfronttwochars, txtoldpassword.text ) == strpassword )
   81 {//密码符合
   82 bpasswordright = true;
   83 }
   84 else
   85 {//密码不符
   86 bpasswordright = false;
   87 }
   88 }
   89 else if( strpassword == txtoldpassword.text ) //原密码为空
   90 {
   91 bpasswordright = true;
   92 }
   93 else
   94 {
   95 bpasswordright = false;
   96 }
   97
   98 if( bpasswordright )
   99 {
  100 //密码正确,写入新的密码,并设置自动加载新的设置,以便下一次更改时仍有效
  101 ini.writestring( strsectionvalue, "password", createcrypassword( getrandomstring(), txtnewpassword.text ) );
  102 controlmessage.innerhtml = "完成密码修改";
  103 }
  104 else
  105 {
  106 controlmessage.innerhtml = "原密码错误";
  107 }
  108
  109 }
   以上代码中的_strservudaemonpath变量用于保存servudaemon.ini文件所在的路径,该值可以在pageload事件中通过web.config设置取得。
   但事情并没有就此结束。经过测试,发现在存在一个严重的问题:修改密码后只有重启serv-u才能使修改后的密码生效。那不是等于没什么用嘛,管理员总不可能没事老在那里重启服务器来使密码修改生效吧。
   再次回来serv-u的官方知识库,查到了如下一条内容:
  manually updating the servudaemon.ini file
  whenever changes are made directly to the servudaemon.ini file, add the following line under the global area in the ini file.
  
  reloadsettings=true
  
  serv-u regularly checks the ini file for this setting. if it is present, serv-u will refresh all stored settings for every domain on the server. this allows serv-u to recognize the changes without having to be restarted.
  
  after serv-u loads the changes, it removes the "reloadsettings=true" entry. this allows you to enter it again next time any changes are made.
  也就是说,只要在ini文件的global节添加键reloadsettings并设置它的值为true便可以实现修改密码后自动更新了。于是只要修改原代码,在101行和102行之间插入如下一句代码即可:
  ini.writestring( "global", "reloadsettings", "true" );
   到这里,一个在线修改serv-u密码的网页就全部完成了。
   程序中的inifile是一个封装了api对ini文件操作的类,仅需要实现对字符串的读取和写入即可。
    


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons