c#web窗体登录界面登录注册以及密码找回发送邮箱功能

c#web窗体登录界面登录注册以及密码找回发送邮箱功能

效果图如下:






1.登录界面aspx代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="login.aspx.cs" Inherits="ebook.Pages.login" %>

<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script src="../Scripts/jquery-1.8.2.min.js"></script> <title></title> <style type="text/css"> .auto-style1 { width: 300px; } .auto-style2 { width: 300px; } .auto-style2 tr td:first-child { //tr里面第一个td text-align: right; width: 100px; } .auto-style2 tr td:last-child { //tr里面最后一个td text-align: left; } </style> <script> $(function () { $("#forgetPwd").click(function () { //登录界面忘记密码 $("#LoginSign").css("display", "none"); //登录界面隐藏 $("#FindPwd").css("display", "block"); //找回密码界面显示 }); $("#BtnfindPwd").click(function () { //找回密码界面的 找回密码按钮事件 if ($("#mail").val() == "") { $("#errormsg").text("邮箱不能为空"); } else { $("#errormsg").text(""); //取消后面span标签的内容显示 $.post("../Handler/FindPwdHandler.ashx", { "mail": $("#mail").val() }, function success(data) { if (data == "NO") { $("#errormsg").text("邮箱不存在"); //输入的邮箱不存在 } else { alert("邮箱发送成功!"); } }); } }); }) </script> </head> <body> <form id="form1" runat="server"> <div id="LoginSign"> <table class="auto-style1"> <tr> <td style="text-align: center"> <asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Names="楷体" Font-Size="XX-Large" ForeColor="#3366FF" Text="用户登录"></asp:Label> </td> </tr> <tr> <td> </td> </tr> <tr> <td> <table class="auto-style2"> <tr> <td> <asp:Label ID="Label2" runat="server" Text="用户名:"></asp:Label> </td> <td> <asp:TextBox ID="txt_User_Name" runat="server" TabIndex="1"></asp:TextBox> </td> </tr> <tr> <td> <asp:Label ID="Label3" runat="server" Text="密码:"></asp:Label> </td> <td> <asp:TextBox ID="txt_User_Pwd" runat="server" TabIndex="2"></asp:TextBox> <a href="javascript:void(0)" id="forgetPwd">忘记密码</a> </td> </tr> <tr> <td> <asp:Button ID="btn_Login" runat="server" TabIndex="3" Text="登录" OnClick="btn_Login_Click" /> </td> <td> <asp:Button ID="btn_Register" runat="server" TabIndex="4" Text="注册" OnClick="btn_Register_Click" /> </td> </tr> <tr> <td> </td> <td> <asp:Label ID="Labinfo" runat="server" Font-Size="Smaller" ForeColor="Red"></asp:Label> //显示登录成功失败的提示 </td> </tr> </table> </td> </tr> </table> </div> <div id="FindPwd" style="display: none"> <table> <tr> <td> <asp:Label ID="Label5" runat="server" Text="邮箱:"></asp:Label><asp:TextBox ID="mail" runat="server"></asp:TextBox><span id="errormsg"></span></td> </tr> <tr> <td> <input type="button" value="找回密码" id="BtnfindPwd" /></td> </tr> </table> </div> </form> </body> </html>
2.登录界面aspx.cs代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using MODEL;
using ebook.App_Start;
using System.IO;
using System.Text;
namespace ebook.Pages
{
    public partial class login : System.Web.UI.Page
    {
        HuiyuanTablemodel model = new HuiyuanTablemodel();
        Logincheckbll bll = new Logincheckbll();
        protected void Page_Load(object sender, EventArgs e)
        {
           
        }

        //登录
        protected void btn_Login_Click(object sender, EventArgs e)
        {
            model.huiyuanming = this.txt_User_Name.Text.ToString();
            model.password = this.txt_User_Pwd.Text.ToString();
            if(bll.logincheck(model))
            {
                Labinfo.Text = "登录成功";
               // Session["Username"] = this.txt_User_Name.Text.ToString();
               // Session[Common.Username] = this.txt_User_Name.Text.ToString();
            }
            else
            {
                this.txt_User_Name.Focus();
                Labinfo.Text = "登录失败";
            }

         
        }

        //注册
        protected void btn_Register_Click(object sender, EventArgs e)
        {
            Response.Redirect("register.aspx");     //页面跳转
        }
    }
}

3.注册页面aspx代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="register.aspx.cs" Inherits="ebook.Pages.register" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <title></title>
    <style type="text/css">
        .auto-style1 {
            width: 600px;
        }

        #table2 tr td:first-child {
            width: 100px;
            text-align: right;
        }

        #table2 tr td:last-child {
            text-align: left;
        }

        #table2 th {
            width: 220px;
            text-align: left;
        }

        .auto-style2 {
            height: 23px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <table align="center" class="auto-style1">
            <tr>
                <td style="text-align: center">
                    <asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Names="楷体" Font-Size="XX-Large" ForeColor="#3366FF" Text="用户注册"></asp:Label>
                </td>
            </tr>
            <tr>
                <td> </td>
            </tr>
            <tr>
                <td>
                    <table class="auto-style1" id="table2">
                        <tr>
                            <td>
                                <asp:Label ID="Label2" runat="server" Text="用户名:"></asp:Label>
                            </td>
                            <th>
                                <asp:TextBox ID="txt_User_Name" runat="server" TabIndex="1"></asp:TextBox>
                                *</th>
                            <td>
                                <asp:Button ID="btn_Check" runat="server" OnClick="btn_Check_Click" TabIndex="2" Text="检测用户名" />
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txt_User_Name" ErrorMessage="RequiredFieldValidator" ForeColor="Red">此项必填</asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <asp:Label ID="Label3" runat="server" Text="密码:"></asp:Label>
                            </td>
                            <th>
                                <asp:TextBox ID="txt_User_Pwd" runat="server" TabIndex="3"></asp:TextBox>
                                *</th>
                            <td>
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txt_User_Pwd" ErrorMessage="RequiredFieldValidator" ForeColor="Red">此项必填</asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <td class="auto-style2">
                                <asp:Label ID="Label4" runat="server" Text="确认密码:"></asp:Label>
                            </td>
                            <th class="auto-style2">
                                <asp:TextBox ID="txt_ReUser_Pwd" runat="server" TabIndex="4"></asp:TextBox>
                                *</th>
                            <td class="auto-style2">
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="txt_ReUser_Pwd" ErrorMessage="RequiredFieldValidator" ForeColor="Red">此项必填</asp:RequiredFieldValidator>
                                <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToCompare="txt_User_Pwd" ControlToValidate="txt_ReUser_Pwd" ErrorMessage="CompareValidator">两次输入的密码不一致</asp:CompareValidator>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <asp:Label ID="Label5" runat="server" Text="姓名:"></asp:Label>
                            </td>
                            <th>
                                <asp:TextBox ID="txt_Rel_Name" runat="server" TabIndex="5"></asp:TextBox>
                                *</th>
                            <td>
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txt_Rel_Name" ErrorMessage="RequiredFieldValidator" ForeColor="Red">此项必填</asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <asp:Label ID="Label6" runat="server" Text="性别:"></asp:Label>
                            </td>
                            <th>
                                <asp:DropDownList ID="DDL_Sex" runat="server" TabIndex="6">
                                    <asp:ListItem>男</asp:ListItem>
                                    <asp:ListItem>女</asp:ListItem>
                                </asp:DropDownList>
                            </th>
                            <td> </td>
                        </tr>
                        <tr>
                            <td>
                                <asp:Label ID="Label7" runat="server" Text="出生日期:"></asp:Label>
                            </td>
                            <th>
                                <asp:DropDownList ID="DDL_Year" runat="server" TabIndex="7">
                                </asp:DropDownList>年
                             
                                <asp:DropDownList ID="DDL_Month" runat="server" TabIndex="8">
                                </asp:DropDownList>月
                             
                                <asp:DropDownList ID="DDL_Day" runat="server" TabIndex="9">
                                </asp:DropDownList>日
                            </th>
                            <td> </td>
                        </tr>
                        <tr>
                            <td>
                                <asp:Label ID="Label8" runat="server" Text="联系地址:"></asp:Label>
                            </td>
                            <th>
                                <asp:TextBox ID="txt_Address" runat="server" TabIndex="10"></asp:TextBox>
                            </th>
                            <td> </td>
                        </tr>
                        <tr>
                            <td>
                                <asp:Label ID="Label9" runat="server" Text="邮政编码:"></asp:Label>
                            </td>
                            <th>
                                <asp:TextBox ID="txt_Postalcode" runat="server" TabIndex="11"></asp:TextBox>
                            </th>
                            <td>
                                <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txt_Postalcode" ErrorMessage="RegularExpressionValidator" ValidationExpression="\d{6}">邮政编码不符合</asp:RegularExpressionValidator>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <asp:Label ID="Label10" runat="server" Text="联系电话:"></asp:Label>
                            </td>
                            <th>
                                <asp:TextBox ID="txt_Tel" runat="server" TabIndex="12"></asp:TextBox>
                                *</th>
                            <td>
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="txt_Tel" ErrorMessage="RequiredFieldValidator" ForeColor="Red">此项必填</asp:RequiredFieldValidator>
                                <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ControlToValidate="txt_Tel" ErrorMessage="RegularExpressionValidator" ValidationExpression="^[1][358][0-9]{9}$">联系电话格式不符合</asp:RegularExpressionValidator>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <asp:Label ID="Label11" runat="server" Text="手机:"></asp:Label>
                            </td>
                            <th>
                                <asp:TextBox ID="txt_Mobile" runat="server" TabIndex="13"></asp:TextBox>
                            </th>
                            <td> </td>
                        </tr>
                        <tr>
                            <td>
                                <asp:Label ID="Label14" runat="server" Text="邮箱:"></asp:Label>
                            </td>
                            <th>
                                <asp:TextBox ID="txt_mail" runat="server" TabIndex="15"></asp:TextBox>
                            </th>
                            <td class="auto-style2">
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" ControlToValidate="txt_mail" ErrorMessage="RequiredFieldValidator" ForeColor="Red">此项必填</asp:RequiredFieldValidator>
                                <asp:RegularExpressionValidator ID="RegularExpressionValidator4" runat="server" ControlToValidate="txt_mail" ErrorMessage="RegularExpressionValidator" ValidationExpression="^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$">邮箱格式不符</asp:RegularExpressionValidator>
                            </td>
                        </tr>
                        <tr>
                            <td class="auto-style2">
                                <asp:Label ID="Label12" runat="server" Text="身份证号:"></asp:Label>
                            </td>
                            <th class="auto-style2">
                                <asp:TextBox ID="txt_ID_Card" runat="server" TabIndex="14"></asp:TextBox>
                                *</th>
                            <td class="auto-style2">
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ControlToValidate="txt_ID_Card" ErrorMessage="RequiredFieldValidator" ForeColor="Red">此项必填</asp:RequiredFieldValidator>
                                <asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server" ControlToValidate="txt_ID_Card" ErrorMessage="RegularExpressionValidator" ValidationExpression="\d{17}[\d|X]|\d{15}">身份证号码格式不符</asp:RegularExpressionValidator>
                            </td>
                        </tr>
                        <tr>
                            <td> </td>
                            <th>
                                <asp:Button ID="btn_Register" runat="server" OnClick="btn_Register_Click" TabIndex="15" Text="注册" />
                                <asp:Button ID="btn_Catch" runat="server" OnClick="btn_Catch_Click" TabIndex="16" Text="重填" />
                                <asp:Label ID="Label13" runat="server" Font-Size="Smaller" ForeColor="Red" Text="带*的为必填项"></asp:Label>
                            </th>
                            <td> </td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr>
                <td style="text-align: center">
                    <asp:Label ID="Labinfo" runat="server" Font-Size="Smaller" ForeColor="Red"></asp:Label>
                </td>
            </tr>
        </table>
    </form>
</body>
</html>

4.注册页面aspx.cs代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using BLL;
using MODEL;
using System.Text;
using ebook.Common;
namespace ebook.Pages
{
    public partial class register : System.Web.UI.Page
    {
        Registerbll bll = new Registerbll();
        HuiyuanTablemodel model = new HuiyuanTablemodel();
        static string statu;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)                              //页面初次加载
            {
                #region 初始化年、月、日
                for (int year = 1930; year <= 2008; year++)
                {
                    DDL_Year.Items.Add(year.ToString());
                }
                for (int month = 1; month <= 12; month++)
                {
                    DDL_Month.Items.Add(month.ToString());
                }
                for (int day = 1; day <= 31; day++)
                {
                    DDL_Day.Items.Add(day.ToString());
                }
                #endregion
                //按钮不触发验证事件
                this.btn_Catch.CausesValidation = false;   //注册事件
                this.btn_Check.CausesValidation = false;   //检测用户名事件
            }
        }
        //注册
        protected void btn_Register_Click(object sender, EventArgs e)
        {
            #region 获取输入表的信息
            model.huiyuanming = this.txt_User_Name.Text.ToString();
            //MD5加密
            MD5 a = new MD5();  //错误方法
            string md5Pwd = a.jiamiMD5(this.txt_User_Pwd.Text.ToString());
            model.password = md5Pwd;
            model.name = this.txt_Rel_Name.Text.ToString();
            model.sex = DDL_Sex.SelectedIndex.ToString();//0代表男   1代表女
            StringBuilder brithday1 = new StringBuilder();
            brithday1.Append(DDL_Year.SelectedItem);
            brithday1.Append("-");
            brithday1.Append(DDL_Month.SelectedItem);
            brithday1.Append("-");
            brithday1.Append(DDL_Day.SelectedItem);
            model.brithday = Convert.ToDateTime(brithday1.ToString());
            model.address = this.txt_Address.Text.ToString();
            model.postal = this.txt_Postalcode.Text.ToString();
            model.phone = this.txt_Tel.Text.ToString();
            model.telphone = this.txt_Mobile.Text.ToString();
            model.idcard = this.txt_ID_Card.Text.ToString();
            model.mail = this.txt_mail.Text.ToString();
            #endregion
            //statu有null,true,false三个状态,null代表没有检测用户名,直接注册提交,true代表检测用户名成功,注册。 false代表检测用户名失败,
            if (statu == null)
            {
                string name = txt_User_Name.Text;
                if (bll.Search(name))
                {
                    Labinfo.Text = "对不起,此用户已经被注册,请输入其他用户名!";
                }
                else
                {
                    if (bll.insert(model))
                    {
                        Response.Write("<script>alert('注册成功!')</script>");
                    }
                    else
                    {
                        Response.Write("<script>alert('注册失败!')</script>");
                    }
                }
            }
            else if (statu == "true")
            {
                if (bll.insert(model))
                {
                    Response.Write("<script>alert('注册成功!')</script>");
                    statu = null;
                }
                else
                {
                    Response.Write("<script>alert('注册失败!')</script>");
                }
            }
            else
            {
                Response.Write("<script>alert('用户名相同!')</script>");
            }

        }

        //重填
        protected void btn_Catch_Click(object sender, EventArgs e)
        {
            this.txt_User_Name.Focus();
            this.txt_User_Name.Text = "";
            this.txt_User_Pwd.Text = "";
            this.txt_ReUser_Pwd.Text = "";
            this.txt_Rel_Name.Text = "";
            this.txt_Address.Text = "";
            this.txt_Postalcode.Text = "";
            this.txt_ID_Card.Text = "";
            this.txt_Mobile.Text = "";
            this.txt_Tel.Text = "";
            this.DDL_Sex.SelectedIndex = 0;
            this.DDL_Year.SelectedIndex = 0;
            this.DDL_Month.SelectedIndex = 0;
            this.DDL_Day.SelectedIndex = 0;
        }

        //检测用户名
        protected void btn_Check_Click(object sender, EventArgs e)
        {
            string name = txt_User_Name.Text;
            if (bll.Search(name))
            {
                Labinfo.Text = "对不起,此用户已经被注册,请输入其他用户名!";
                statu = "false";
            }
            else
            {
                Labinfo.Text = "恭喜您,此用户名可以使用!";
                statu = "true";
            }
        }
    }
}

4.找回密码,发送到邮箱

  <div id="FindPwd" style="display: none">
            <table>
                <tr>
                    <td>
                        <asp:Label ID="Label5" runat="server" Text="邮箱:"></asp:Label><asp:TextBox ID="mail" runat="server"></asp:TextBox><span id="errormsg"></span></td>
                </tr>
                <tr>
                    <td>
                        <input type="button" value="找回密码" id="BtnfindPwd" /></td>
                </tr>
            </table>
        </div>

 <script>
        $(function () {
            $("#forgetPwd").click(function () {
                $("#LoginSign").css("display", "none");
                $("#FindPwd").css("display", "block");
            });
            $("#BtnfindPwd").click(function () {
                if ($("#mail").val() == "") {
                    $("#errormsg").text("邮箱不能为空");
                }
                else {
                    $("#errormsg").text("");
                    $.post("../Handler/FindPwdHandler.ashx", { "mail": $("#mail").val() }, function success(data) {
                        if (data == "NO") {
                            $("#errormsg").text("邮箱不存在");
                        }
                        else {
                            alert("邮箱发送成功!");
                        }
                    });
                }
            });
        })
    </script>


5 .../Handler/FindPwdHandler.ashx代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BLL;
using System.Net.Mail;  //邮箱发送的引用文件
using System.Net;       //身份验证的引用文件
using System.Text;
using ebook.Common;

namespace ebook.Handler
{
    /// <summary>
    /// FindPwdHandler 的摘要说明
    /// </summary>
    public class FindPwdHandler : IHttpHandler
    {
        Checkmail checkmail = new Checkmail();

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string mail = context.Request["mail"];
            if (checkmail.CheckmailISnull(mail))            //mail存在,发送邮件
            {
                sendMail(mail);
            }
            else
            {
                context.Response.Write("NO");
            }
        }

        private void sendMail(string mail)
        {
            string newPwd = Guid.NewGuid().ToString().Substring(0, 6);           //随机创建一个新的密码
            string userid = checkmail.GetuseridBymail(mail);                     //通过邮箱找到账户名称
            MD5 md5 = new MD5();
            if (updatePwd(userid, md5.jiamiMD5(newPwd)))                         //密码更新成功在发送邮件
            {
                try
                {
                    MailMessage mailMsg = new MailMessage();                                 //MailMessage引用using System.Net.Mail;
                    mailMsg.From = new MailAddress("xxxxx@163.com", "小红");                 //源邮件地址
                    mailMsg.To.Add(new MailAddress("xxxxx@163.com", "asd"));                 //接收人地址
                    mailMsg.SubjectEncoding = Encoding.GetEncoding(936);                     //这里非常重要,如果你的邮件标题包含中文,这里一定要指定,否则对方收到的极有可能是乱码。
                    mailMsg.Subject = "找回密码";                                            //标题
                    mailMsg.BodyEncoding = Encoding.GetEncoding(936);                        //邮件正文的编码, 设置不正确, 接收者会收到乱码
                    StringBuilder sb = new StringBuilder();
                    sb.Append("您在xxx网修改密码");
                    sb.Append("您的ID:" + userid);
                    sb.Append("密码:" + newPwd);
                    mailMsg.Body = sb.ToString();                                            //邮件正文
                    mailMsg.IsBodyHtml = true;                                               //邮件正文是否是HTML格式
                    //指定Smtp服务地址
                    SmtpClient Client = new SmtpClient("smtp.163.com");                      //smtp.136.com smtp.qq.com
                    Client.Credentials = new NetworkCredential("xxxxxx@163.com", "xxxxx");   //NetworkCredential引用using System.Net; 发件人的邮箱和密码
                    Client.Send(mailMsg);
                }
                catch (Exception ex)
                {

                    throw ex;
                }
            }
            else
            {
                //不发送邮箱,因为密码更新失败!!
            }

        }

 
        private bool updatePwd(string userid, string Pwd)               //更新旧密码
        {
            if (checkmail.updatePwd(userid, Pwd))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}





  • 12
    点赞
  • 105
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值