Hello forum,
My email confirmation link does not redirect to the activation page. After successful registration, an activation link is sent to user’s email but when user clicks on the activation link, it redirects the user back to the registration page.
This is the line of code where the activation link to the page is
body += "<br /><a href = '" + Request.Url.AbsoluteUri.Replace("CS.aspx", "CS_Activation.aspx?ActivationCode=" + activationCode) + "'>Click here to activate your account.</a>";
Here is my full code (SingUp.aspx)
private void SendActivationEmail(int Uid)
{
string activationCode = Guid.NewGuid().ToString();
using (SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Dataregister.mdf;Integrated Security=True"))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO UserActivation VALUES(@Uid, @ActivationCode)"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@Uid", Uid);
cmd.Parameters.AddWithValue("@ActivationCode", activationCode);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
using (MailMessage mm = new MailMessage("georgeakpan13@gmail.com", mailtxtbx.Text))
{
mm.Subject = "Account Activation";
string body = "Hello " + txtname.Text.Trim() + ",";
body += "<br /><br />Please click the following link to activate your account";
body += "<br /><a href = '" + Request.Url.AbsoluteUri.Replace("CS.aspx", "CS_Activation.aspx?ActivationCode=" + activationCode) + "'>Click here to activate your account.</a>";
body += "<br /><br />Thanks";
mm.Body = body;
mm.IsBodyHtml = true;
SmtpClient SMTP = new SmtpClient("smtp.gmail.com", 587);
SMTP.UseDefaultCredentials = false;
SMTP.Credentials = new NetworkCredential()
{
UserName = "georgeakpan13@gmail.com",
Password = "IDONGete2"
};
SMTP.EnableSsl = true;
SMTP.Send(mm);
}
}
Activation page code (CS_Activation.aspx)
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string activationCode = !string.IsNullOrEmpty(Request.QueryString["ActivationCode"]) ? Request.QueryString["ActivationCode"] : Guid.Empty.ToString();
using (SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Dataregister.mdf;Integrated Security=True"))
{
using (SqlCommand cmd = new SqlCommand("DELETE FROM UserActivation WHERE ActivationCode = @ActivationCode"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@ActivationCode", activationCode);
cmd.Connection = con;
con.Open();
int rowsAffected = cmd.ExecuteNonQuery();
con.Close();
if (rowsAffected == 1)
{
ltMessage.Text = "Activation successful.";
}
else
{
ltMessage.Text = "Invalid Activation code.";
}
}
}
}
}
}