注册 | 登录 忘记密码? 51cto首页 | 博客 | 论坛 | 招聘
热点文章 Cisco IOS下载
 帮助

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


2008-01-10 15:18:19
 标签:.NET ASP 加密 验证 MD5   [推送到技术圈]

版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://zhcsmx22.blog.51cto.com/287943/58718
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 = "错误";
        }
    }

本文出自 “赵宏臣主页” 博客,请务必保留此出处http://zhcsmx22.blog.51cto.com/287943/58718





    文章评论
 
 

发表评论

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