ASP.Net登录窗体与Cookie使用MD5加密

版权声明:转载原创文章请以超链接形式请注明原文章出处,尊重作者,尊重原创!


恰饭广告




实现效果:

实现效果

MD5加密后的cookie信息:

cookie信息

数据库表结构:

数据库表

注意:新建Login.aspx和Success.aspx,Login.aspx拖拽两个TextBox控件和一个Button控件;Success.aspx拖拽一个Button控件

Login.aspx.cs代码:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication5
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Cookies["login"] != null)
            {
                Response.Redirect("Success.aspx");
            }
        }
        //cookie加密
        private string md5(string name)
        {
            System.Security.Cryptography.MD5CryptoServiceProvider md5CSP = new System.Security.Cryptography.MD5CryptoServiceProvider();
            //获取要加密的字段,并转化为Byte[]数组
            byte[] testEncrypt = System.Text.Encoding.Unicode.GetBytes(name);
            //加密Byte[]数组
            byte[] resultEncrypt = md5CSP.ComputeHash(testEncrypt);
            //将加密后的数组转化为字段(普通加密)乱码
            //string EncryptPWD = System.Text.Encoding.Unicode.GetString(resultEncrypt);
            string EncryptPWD = FormsAuthentication.HashPasswordForStoringInConfigFile(name, "MD5");
            return EncryptPWD;
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            string username = this.TextBox1.Text.Trim();
            string password = this.TextBox2.Text.Trim();
            string testDB = ConfigurationManager.ConnectionStrings["testDB"].ConnectionString;
            SqlConnection conn = new SqlConnection(testDB);
            conn.Open();
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = "select * from users where username=@username and password=@password";
            cmd.Parameters.AddWithValue("@username", username);
            cmd.Parameters.AddWithValue("@password", password);
            SqlDataReader dr = cmd.ExecuteReader();
            if (username.Equals("") || password.Equals(""))//用户名或密码为空
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "alert('不能为空');", true);
            }
            else
            {
                if (dr.Read())
                {
                    HttpCookie cookie = new HttpCookie("login");
                    cookie.Values["username"] = md5(username);//保存用户名  
                    cookie.Values["password"] = md5(password) ;//保存密码  
                    cookie.Expires = DateTime.Now.AddSeconds(60); //cookie失效时间一分钟
                    Response.Cookies.Add(cookie);
                    Response.Redirect("Success.aspx");
                    //ClientScript.RegisterStartupScript(this.GetType(), "", "alert('登录成功');", true);
                }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "alert('登录失败');", true);
                }
            }
            dr.Close();
            conn.Close();
        }
    }
}

Success.aspx.cs代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication5
{
    public partial class Success : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Cookies["login"] == null)
            {
                Response.Redirect("Login.aspx");
            }
            else
            {
                string username = Request.Cookies["login"]["username"];
                Response.Write("管理员" + "成功登录!");
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            //减去cookie时间即为失效
            HttpCookie cookie = new HttpCookie("login");
            cookie.Expires = DateTime.Now.AddDays(-1);
            Response.Cookies.Add(cookie);
        }
    }
}

参考文档(MD5加密方式):https://blog.csdn.net/ChessPlayer/article/details/1722826

原文链接:https://www.idaobin.com/archives/1143.html

让我恰个饭吧.ヘ( ̄ω ̄ヘ)

支付宝 ——————- 微信
图片加载中图片加载中



恰饭广告

发表评论

电子邮件地址不会被公开。 必填项已用*标注

23 − = 13