当前位置:首页 > 网络编程 > WEB编程 > ASP.net >  为Serv-U提供在线修改密码功能(2)

 为Serv-U提供在线修改密码功能(2)

点击次数:29 次 发布日期:2008-11-26 14:18:06 作者:源代码网
源代码网推荐      用户密码的加密方法可以在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文件操作的类,仅需要实现对字符串的读取和写入即可。
源代码网推荐    做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。
源代码网推荐


源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华