用XML代替數據庫來實現WEB的窗體認証

王朝other·作者佚名  2006-01-09
宽屏版  字体: |||超大  

最近看了一些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﹑其中的密碼是用了哈稀算法。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有