日期:2008-07-26  浏览次数:20421 次

最近看了一些C#操作XML的資料﹐感覺與操作Sql等沒有什么區別﹐于是簡單地寫了一個加密的用于實現登入Web網頁在認証程序,供大家參考:
1﹑在Form中添加兩個TextBox和一個Button控件﹐一個TextBox ID為UserName,一個為UserPwd.2﹑在Form中引入System.Web.Security和System.IO兩個類.3﹑開始寫Button的響應代碼: private void OK_Click(object sender, System.EventArgs e) { string cmd="UserName='"+UserName.Text.Trim()+"'"; DataSet ds=new DataSet(); FileStream fs=new FileStream(Server.MapPath("Users.XML"),FileMode.Open,FileAccess.Read); StreamReader reader=new StreamReader(fs); ds.ReadXML(reader); fs.Close(); DataTable users=ds.Tables[0]; DataRow[] matches=users.Select(cmd); if(matches!=null && matches.Length>0) { DataRow row=matches[0]; string hashedpwd=FormsAuthentication.HashPasswordForStoringInConfigFile(UserPwd.Text.Trim(),"SHA1"); string pass=(string)row["UserPassword"]; if(string.Compare(pass,hashedpwd,false)!= 0) { Response.Write("密碼不正確!"); } else { FormsAuthentication.SetAuthCookie(UserEmail.Text,false); Response.Ridirect(“default.ASPx“); } } else { Response.Write(“無此用戶!“); } }3﹑其中的密碼是用了哈稀算法。