如何通过发送验证电子邮件来批准用户

时间:2016-02-04 10:41:20

标签: asp.net sql-server query-string

我在通过电子邮件验证用户时遇到问题...这是我的注册页面代码..

protected void btnsignup_Click(object sender, EventArgs e)
    {

        MailMessage msg;
        //SqlCommand cmd = new SqlCommand();
        string ActivationUrl = string.Empty;
        string emailId = string.Empty;
        try
        {

            //Sending activation link in the email
            msg = new MailMessage();
            SmtpClient smtp = new SmtpClient();
            emailId = txtemail.Text.Trim();
            //sender email address
            msg.From = new MailAddress("xyz@gmail.com");
            //Receiver email address
            msg.To.Add(emailId);
            msg.Subject = "Confirmation email for account activation";
            //For testing replace the local host path with your lost host path and while making online replace with your website domain name
            ActivationUrl = Server.HtmlEncode("http://localhost:51049/ActivateAccount.aspx?CustomerId=" + FetchUserId(emailId) + "&EmailId=" + emailId);

            msg.Body = "Hi " + txtName.Text.Trim() + "!\n" +
                  "Thanks for showing interest and registring in <a href='Home.aspx'> e2e website <a> " +
                  " Please <a href='" + ActivationUrl + "'>click here to activate</a>  your account and enjoy our services. \nThanks!";
            msg.IsBodyHtml = true;
            smtp.Credentials = new NetworkCredential("xyz@gmail.com", "xxxxxx");
            smtp.Port = 587;
            smtp.Host = "smtp.gmail.com";
            smtp.EnableSsl = true;
            smtp.Send(msg);

            if (result > 0)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Confirmation Link to activate your account has been sent to your email address');window.location='LoginPage.aspx'", true);
                ClearControl(this);
            }
            else
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "popup", "alert('Account has not been created.');'", true);
            }


        }
        catch (Exception ex)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Error occured : " + ex.Message.ToString() + "');", true);
            return;
        }
        finally
        {

            con.Close();
        }
    }


    protected string FetchUserId(string emailId)
    {

        SqlCommand cmd = new SqlCommand("select CustomerId from tbl_CustomerRegistration WHERE CustomerEmail=@CustomerEmail", con);
        cmd.Parameters.AddWithValue("@CustomerEmail", emailId);
        string CustomerId = cmd.ExecuteScalar().ToString();
        return CustomerId;
    }

和ActivateMyAccount.aspx的代码...

SqlCommand cmd = new SqlCommand();         尝试         {

        if ((!string.IsNullOrEmpty(Request.QueryString["CustomerId"])) & (!string.IsNullOrEmpty(Request.QueryString["CustomerEmail"])))
        {   
            //approve account by setting Is_Approved to 1 i.e. True in the sql server table

            cmd = new SqlCommand("UPDATE tbl_CustomerRegistration SET Is_Approved=1 WHERE CustomerId=@CustomerId AND CustomerEmail=@CustomerEmail", con);
            cmd.Parameters.AddWithValue("@CustomerId", Request.QueryString["CustomerId"]);
            cmd.Parameters.AddWithValue("@CustomerEmail", Request.QueryString["CustomerEmail"]);

            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }

           int result= cmd.ExecuteNonQuery();
            if(result>0)
            {
                 ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Your account has been activated. You can login now');window.location='LoginPage.aspx'", true);
        }

        }
    }
    catch (Exception ex)
    {
        ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Error occured : " + ex.Message.ToString() + "');", true);
        return;
    }
    finally
    {
        con.Close();
        cmd.Dispose();
    }
}

用户在注册后收到电子邮件,但问题是当用户点击验证链接时,数据库名称中的字段为Is_Aprroved未获取更新...谢谢你..请帮助我

0 个答案:

没有答案