注册 | 登录 忘记密码? 51cto首页 | 博客 | 论坛 | 招聘
热点文章 Linux全攻略--文件和磁盘管理
 帮助

ASP.NET使用MD5加密用户验证


2007-12-11 17:16:43
版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://binzone.blog.51cto.com/147856/54544
又一个关于MD5的测试实例,通过前台获取用户名和密码。然后与数据库内存储的数据进行验证。
 
使用类型:
using System.Data.SqlClient;
using System.Configuration;
using System.Web.Configuration;
using System.Security.Cryptography;
using System.Text;
 
代码部分:
    protected void Button1_Click(object sender, EventArgs e)
    {
        //建立数据库连接
        string ConnString = WebConfigurationManager.ConnectionStrings["ABCDWebConnectionString"].ConnectionString;
        SqlConnection conn = new SqlConnection(ConnString);
        //创建数据库命令
        string sql;
        sql = "Select COUNT(*) From Users Where UName=@Username and PassW=@Password";
        SqlCommand Comm = new SqlCommand(sql, conn);
        //重建用户名验证
        SqlParameter Username = new SqlParameter("@Username", SqlDbType.NChar, 50);
        Username.Value = TextBox1.Text;
        Comm.Parameters.Add(Username);

        MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
        Byte[] hashedDataBytes;
        UTF8Encoding encoder = new UTF8Encoding();

        hashedDataBytes = md5Hasher.ComputeHash(encoder.GetBytes(TextBox2.Text));

        string b = "";

        for (int i = 0; i <= 15; ++i)
        {
            b += hashedDataBytes[i];
        }

        SqlParameter Password = new SqlParameter("@Password", SqlDbType.NChar, 50);
        Password.Value = b;
        Comm.Parameters.Add(Password);

        conn.Open();
        int j = Convert.ToInt16(Comm.ExecuteScalar());
        conn.Close();

        if (j == 1)
        {
            Label1.Text = "正确";
        }
        else
        {
            Label1.Text = "错误";
        }
    }

本文出自 “bin_zone” 博客,请务必保留此出处http://binzone.blog.51cto.com/147856/54544





    文章评论
 
2007-12-11 17:30:53
恩 简单明了

 

发表评论

昵   称:
验证码:  点击图片可刷新验证码  博客过2级,无需填写验证码
内   容: