.NET简单的登录邮箱验证

虽然现在很多的网站的登录验证都已经改成手机验证了,但邮箱验证还是一个比较主流的验证方式,下面我给大家介绍一个比较简单的邮箱验证的实现方法,大概的思路是:注册的时候需要填写邮箱账户,注册成功后,后台生成一段唯一的key加上用户注册时候的ID进行验证,唯一的Key可以保存到会话中设置会话的过期时间,那么就可以使用户在有限的时间内进行验证!初略的贴出自己的实现代码,建议修正后使用毕竟比较简陋:

前台的页面:
前台的页面

我这个是用MVC做的,贴一下Controller的代码:

#region 注册和邮箱验证 Register() emailVerification() erroVerifition()
        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="userid">登录ID</param>
        /// <param name="password">登录密码</param>
        /// <param name="email">邮箱</param>
        /// <returns></returns>
        [HttpPost]
        public String Register(string userid, string password, string email)
        {

            Regex checkCN = new Regex("[\u4e00-\u9fa5]");
            Match mCheckCN = checkCN.Match(userid);
            Regex checkEm = new Regex(@"[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?");
            Match mCheckem = checkEm.Match(email);
            try
            {
                if (userid.Length < 4 || userid.Length > 15 || mCheckCN.Success == true)
                {
                    return "用户名必须大于4小于5并且不为中文";
                }
                else if (mCheckem.Success == false)
                {
                    return "请输入一个正确的邮箱格式!";
                }
                else if (password.Length < 6 || password.Length > 18)
                {
                    return "密码大于6位小于16位";
                }
                else
                {
                    if (!baseSqlUser.Exist(users => users.userid == userid||users.email==email))
                    {
                        string pass = userHelp.passEncrypt(password);
                        string codeKey = userHelp.getVerification(15);//随机生成的15个key
                        User user = new Easy.Model.User();
                        user.email = email;
                        user.password = pass;
                        user.userid = userid;
                        user.role = "0";//这里表示激活状态
                        user.regTime = DateTime.Now.Date.ToString();
                        user.logIp = Request.UserHostAddress;
                        user.codeKey = codeKey;
                        if (baseSqlUser.Add(user) == true)
                        {
                            if (userHelp.sendEmail(email, codeKey, userid,"1") == true)
                            {
                                return "1";
                            }
                            else
                            {
                                return "发送激活邮件失败,请与管理员联系!";
                            }


                        }
                        else
                        {
                            return "0";
                        }
                    }
                    else
                    {
                        return "用户已存在";
                    }
                }
            }
            catch (Exception e)
            {
                return e.ToString();

            }
        }
        /// <summary>
        /// 邮箱验证
        /// </summary>
        /// <returns></returns>
        public ActionResult emailVerification()
        {

            string userid = Request["userid"];
            string veriCode = Request["codeKEY"];
            if (baseSqlUser.Exist(u => u.userid == userid && u.codeKey == veriCode) == true)
            {
                User upUser = new User();
                upUser = baseSqlUser.Find(u => u.userid == userid && u.codeKey == veriCode);
                upUser.role = "1";//修改激活状态
                if (baseSqlUser.Update(upUser) == true)//这里是对数据进行更新
                {
                    return View();//返回验证成功的页面
                }
                else
                {
                    return Redirect("");//这里可以重定向一个错误页面
                }

            }
            else
            {
                return Redirect("");
            }

        }
        /// <summary>
        /// 返回错误
        /// </summary>
        /// <returns></returns>
        public ActionResult erroVerifition()
        {
            return View();
        }

        #endregion

我这里并没有用session之类的来保存Key,而是直接插入了数据库,这样做不太好。

发送邮件的方法:

   public bool sendEmail(string email,string codeKey,string userid,string type)
        {
            string formto = "发送的邮箱账户";
            string to = email;   //接收邮箱
            string content = "邮箱主题名";
            string body = "";
            if (type == "1")
            {
                body = "您好,感谢您在xpzzs注册帐户!激活帐户需要点击下面的链接:<br/><a href=\"网站链接/UserRelevant/emailVerification?" + "codeKey=" + codeKey + "&userid=" + userid + "\" >mail.xpzzs.top/UserRelevant/emailVerification?codeKey=" + codeKey + "&userid=" + userid + "</a>";

            }
            else
            {
                body = "您好,感谢您在xpzzs注册帐户!点击下面的链接修改密码:<br/><a href=\"网站链接/UserRelevant/updatePassWord?" + "codeKey=" + codeKey + "&userid=" + userid + "\" >mail.xpzzs.top/UserRelevant/updatePassWord?codeKey=" + codeKey + "&userid=" + userid + "</a>";
            }

            string name = "邮箱账户名";
            string upass = "邮箱密码";
            string smtp = "smtp.qq.com";//发送的服务器
            SmtpClient _smtpClient = new SmtpClient();
            _smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式
            _smtpClient.Host = smtp; //指定SMTP服务器
            _smtpClient.EnableSsl = true;
            _smtpClient.UseDefaultCredentials = false;  
            _smtpClient.Credentials = new System.Net.NetworkCredential(name, upass);//用户名和密码
            MailMessage _mailMessage = new MailMessage();
            //发件人,发件人名 
            _mailMessage.From = new MailAddress(formto, "xpzzs");
            //收件人 
            _mailMessage.To.Add(to);
            _mailMessage.SubjectEncoding = System.Text.Encoding.GetEncoding("gb2312");
            _mailMessage.Subject = content;//主题
            _mailMessage.IsBodyHtml = true;
            _mailMessage.Body = body;//内容
            _mailMessage.BodyEncoding = System.Text.Encoding.GetEncoding("gb2312");//正文编码

            _mailMessage.Priority = MailPriority.High;//优先级   
            try
            {
                _smtpClient.Send(_mailMessage);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

KEY生成代码:

  /// <summary>
        /// 获取验证码字符串
        /// </summary>
        /// <param name="length">验证码的字数</param>
        /// <returns></returns>
        public string getVerification(int length)
        {
            char[] ver = new char[length];
            string Verification = "";
            char[] dictionary = { 'a', 'b', 'c', 'c', 'd', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'w', 'y', 'z', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
            Random radm = new Random();
            for (int i = 0; i < length; i++)
            {
                Verification = Verification + dictionary[radm.Next(dictionary.Length - 1)];
            }
            return Verification;
        }

大概是这样,做的有点简陋需要的朋友可以自己尝试去完善!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值