コード例 #1
0
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string chkCode = txtChkCode.Value + "";
        string valCode = hdValCode.Value + "";

        if (chkCode != "")
        {
            if (valCode == "")
            {
                return;
            }
            if (LFL.Utility.Security.MD5Util.ToMD5(valCode).Replace("-", "").Replace("A", "").Replace("B", "").Replace("C", "").Replace("D", "").Replace("E", "").Replace("F", "").Substring(0, 4) != chkCode)
            {
                AlertAndBack("验证码错误!");
            }
        }

        LFL.Favorite.Model.User user = new LFL.Favorite.Model.User();


        string strUsername, strPassword, strRePassword, strEmail;

        strUsername = txtUsername.Value.Trim();
        if (string.IsNullOrEmpty(strUsername) || strUsername.Length < 4 || strUsername.Length > 20)
        {
            AlertAndBack("用户名不能为空且长度于5-20之间!");
        }
        strPassword = txtPassword.Value.Trim();
        if (string.IsNullOrEmpty(strPassword) || strPassword.Length < 6 || strPassword.Length > 50)
        {
            AlertAndBack("密码不能为空且长度于6-50之间!");
        }
        strRePassword = txtRePassword.Value.Trim();
        if (string.IsNullOrEmpty(strRePassword) || !strPassword.Equals(strRePassword))
        {
            AlertAndBack("两次密码不一致!");
        }
        strEmail = txtEmail.Value.Trim();
        if (string.IsNullOrEmpty(strEmail))
        {
            AlertAndBack("邮箱不能为空!");
        }

        if (!chkAgreement.Checked)
        {
            AlertAndBack("请仔细阅读注册协议。");
        }

        LFL.Favorite.BLL.UserBll bll = new LFL.Favorite.BLL.UserBll();
        if (bll.Exists(strUsername, strEmail))
        {
            AlertAndBack("该用户名或邮箱已经被注册!");
        }


        DateTime dtmNow          = DateTime.Now;
        string   strActivateCode = MD5Util.ToMD5(dtmNow.ToString("yyyyss7MMddongmmHH"));

        user.Username     = strUsername;
        user.Nickname     = strUsername;
        user.Password     = MD5Util.ToMD5(strPassword);
        user.Email        = strEmail;
        user.Sex          = "保密";
        user.ActivateCode = strActivateCode;
        user.RegTime      = dtmNow;
        user.RegIP        = WebAgent.GetIP();
        user.LoginCount   = 0;
        user.Level        = 0;
        user.Credit       = 0;
        user.Status       = 0; //


        try
        {
            bll = new LFL.Favorite.BLL.UserBll();
            bll.Add(user);
        }
        catch (Exception ex)
        {
            WebLog.WriteErrLog("注册失败!原因:" + ex.Message);
            AlertAndBack("注册失败!原因:" + ex.Message);
        }

        try
        {
            SendActiveCodeEmail(strEmail, strUsername, strPassword, strActivateCode);
        }
        catch (Exception ex)
        {
            WebLog.WriteErrLog("发送注册邮件失败!原因:" + ex.Message);
            AlertAndBack("发送注册邮件失败!原因:" + ex.Message);
        }

        WebLog.WriteInfoLog("注册用户名:" + user.Username + ", Email:" + user.Email + "成功!");


        //成功
        lblUsername.Text = strUsername;
        lblPassword.Text = strPassword;
        hdRegTime.Value  = dtmNow.ToString("yyyyss7MMddongmmHH");
        regForm.Visible  = false;
        regOk.Visible    = true;
    }