Asp.Net(2):MD5加密登陆模块
03 Jun 2012一个同学让帮忙写作业,MD5加密的登陆。
就用到一个 System.Security.Cryptography;
用jquery加了一个浮动层,身份验证没用ajax。
部分代码:
using System; using System.Web; using System.Web.Security; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient;//数据库操作 using System.Security.Cryptography;//MD5 using System.Data; using System.Configuration; /// <summary> ///DB 的摘要说明 /// </summary> public class DB { public DB() { } /// <summary> /// 连接数据库 /// </summary> /// <returns>返回SqlConnection对象</returns> public SqlConnection GetCon() { return new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString()); } /// <summary> /// 执行SQL语句 /// </summary> /// <param name="cmdstr">SQL语句</param> /// <returns>成功1,失败0</returns> public int SqlEx(string cmdstr) { SqlConnection con = GetCon(); con.Open(); SqlCommand cmd = new SqlCommand(cmdstr,con); try { cmd.ExecuteNonQuery(); return 1; } catch (Exception e) { return 0; } finally { con.Dispose(); } } /// <summary> /// 执行SQL查询语句 /// </summary> /// <param name="cmdstr">SQL查询语句</param> /// <returns>返回DataTable数据表</returns> public DataTable reDt(string cmdstr) { SqlConnection con = GetCon(); SqlDataAdapter da = new SqlDataAdapter(cmdstr,con); DataSet ds = new DataSet(); da.Fill(ds); return ds.Tables[0]; } /// <summary> /// 执行SQL查询语句 /// </summary> /// <param name="str">SQL查询语句</param> /// <returns>返回SqlDataReader对象dr</returns> public SqlDataReader reDr(string str) { SqlConnection conn = GetCon(); conn.Open(); SqlCommand com=new SqlCommand(str,conn); SqlDataReader dr = com.ExecuteReader(CommandBehavior.CloseConnection); return dr; } public string MD5(string strPwd) { MD5 md5 = new MD5CryptoServiceProvider(); //将字符编码为一个字节序列 byte[] data = System.Text.Encoding.Default.GetBytes(strPwd); //计算data字节数组的哈希值 byte[] md5data = md5.ComputeHash(data); md5.Clear(); string str = string.Empty; for (int i = 0; i < md5data.Length-1; i++) { str += md5data[i].ToString("x").PadLeft(2,'0'); } return str; } }
转载请注明:于哲的博客 » Asp.Net(2):MD5加密登陆模块