搭建asp会议签到系统:第一章 账密登录

搭建asp会议签到系统


第一章 账密登录
第二章 生成会议签到二维码
第三章 会议签到
第四章 会议统计



前言

目的:实现账密登录且记录下当前登陆账号(备第三章使用)。


提示:以下是本篇文章正文内容,下面案例可供参考

一、设置跳板

让需登录且未登录的请求页面跳转到login.aspx

<authentication mode="Forms">
  <forms cookieless="UseCookies" name="LoginCookieName" loginUrl="~/Login/Login.aspx" />
</authentication>

在这里插入图片描述

二、创建login.aspx页面

    <div class="container">
	<div class="row clearfix">
		<div class="col-md-12 column">
			<h3>
				请输入您的OA账号密码.
			</h3>
            <asp:PlaceHolder runat="server" ID="ErrorMessage" Visible="true">
             <p class="text-danger">
                 <asp:Literal runat="server" ID="FailureText" />
              </p>
            </asp:PlaceHolder>
			<form role="form">
				<div class="form-group">
				<label for="exampleInputEmail1">账号</label>
                <asp:TextBox runat="server" ID="Email" CssClass="form-control" />
				</div>
			<div class="form-group">
			<label for="exampleInputPassword1">密码</label>
            <asp:TextBox runat="server" ID="Password" TextMode="Password" CssClass="form-control" />
				</div>
                <asp:Button ID="Button1" class="btn btn-info" runat="server" Text="登录" OnClick="LogIn" />
			</form>
		</div>
	</div>
</div>

在这里插入图片描述

三、登录读取

3.1 引用类

using System;
using System.Data.SqlClient;
using System.Security.Cryptography;
using System.Text;
using System.Web.Security;

3.2 加密明文方法

        public static string EncryptToSHA1(string cleanString)
        {
            Byte[] data = Encoding.Default.GetBytes(cleanString);//以字节方式存储
            SHA1 sha1 = new SHA1CryptoServiceProvider();
            Byte[] result = sha1.ComputeHash(data);//得到哈希值
            return BitConverter.ToString(result).Replace("-", ""); //转换成为字符串的显示
        }
        public string EncryptToMD5(string str)
        {
            //初始化MD5对象
            MD5 md5 = MD5.Create();
            //将源字符串转化为byte数组
            Byte[] soucebyte = Encoding.Default.GetBytes(str);
            //soucebyte转化为mf5的byte数组
            Byte[] md5bytes = md5.ComputeHash(soucebyte);
            //将md5的byte数组再转化为MD5数组
            StringBuilder sb = new StringBuilder();
            foreach (Byte b in md5bytes)
            {
                //x表示16进制,2表示2位
                sb.Append(b.ToString("x2"));
            }
            return sb.ToString();
        }

3.3 比对结果

protected void LogIn(object sender, EventArgs e)
        {
            // 把Password输入的明文加密成密文
            string pwd = EncryptToSHA1(Password.Text);
            //防止SQL注入
            string userid = Email.Text.Trim().Replace("'", "");
            string UserName = "";
            //引用web.config里面的数据库连接字符串
            string strDBConn = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["DC6"].ToString();
            string sql = "你的语句 where UserID='" + userid + "' and password = ('" + pwd + "') ";
            SqlConnection com = new SqlConnection(strDBConn);
            com.Open();
            //定义查询命令:表示对数据库执行一个SQL语句或存储过程
            SqlCommand command = new SqlCommand(sql, com);
            //执行查询:提供一种读取数据库行的方式
            SqlDataReader sread = command.ExecuteReader();
            try
            {
                if (sread.Read() )//如比对成功
                {
             UserName = sread["UserName"].ToString();
             //写入cookie
             FormsAuthentication.SetAuthCookie(userid, true);
             //跳转到登录操作前请求页面
             FormsAuthentication.RedirectFromLoginPage(userid, true);
                }
                else
                {
              FailureText.Text = "账号或密码错误,请重新登录。";
              ErrorMessage.Visible = true;
                }
            }
            catch (Exception)
            {
             Response.Write("<script>alert('连接数据库失败,请检查网络!')</script>");
                return;
            }
            finally
            {
             com.Close();                    //关闭连接
             com.Dispose();                  //释放连接
             sread.Dispose();                //释放资源
            }
        }

总结

登陆方式很多种,你可以自建一套账密信息,也可以做单点登录。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

载河之舟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值