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

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

效果图如下:
c#web窗体登录界面登录注册以及密码找回发送邮箱功能_第1张图片
c#web窗体登录界面登录注册以及密码找回发送邮箱功能_第2张图片
c#web窗体登录界面登录注册以及密码找回发送邮箱功能_第3张图片
c#web窗体登录界面登录注册以及密码找回发送邮箱功能_第4张图片
c#web窗体登录界面登录注册以及密码找回发送邮箱功能_第5张图片

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

 
忘记密码
  //显示登录成功失败的提示
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代码
c#web窗体登录界面登录注册以及密码找回发送邮箱功能_第6张图片
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="register.aspx.cs" Inherits="ebook.Pages.register" %>





    

    
    


    
 
* 此项必填
* 此项必填
* 此项必填 两次输入的密码不一致
* 此项必填
 
 
 
邮政编码不符合
* 此项必填 联系电话格式不符合
 
此项必填 邮箱格式不符
* 此项必填 身份证号码格式不符
   

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("");
                    }
                    else
                    {
                        Response.Write("");
                    }
                }
            }
            else if (statu == "true")
            {
                if (bll.insert(model))
                {
                    Response.Write("");
                    statu = null;
                }
                else
                {
                    Response.Write("");
                }
            }
            else
            {
                Response.Write("");
            }

        }

        //重填
        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.找回密码,发送到邮箱

  

 


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
{
    /// 
    /// FindPwdHandler 的摘要说明
    /// 
    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("[email protected]", "小红");                 //源邮件地址
                    mailMsg.To.Add(new MailAddress("[email protected]", "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("[email protected]", "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;
            }
        }
    }
}





你可能感兴趣的:(c#web窗体)