Question

I send a confirmation email to my users and as they click on it, their account will become active. The only thing that i want is that to expire the link after 48 hour and user can register again with that username. can anybody help me? here is my email code:

   Session["UserName"] = TextBox_email.Text;

   MailMessage msg = new MailMessage();
   StringBuilder bodyMsg = new StringBuilder();

   MembershipUser user = Membership.CreateUser(TextBox_email.Text, TextBox_Pass.Text, TextBox_email.Text);

   Roles.AddUserToRole(TextBox_email.Text, "Author");
   user.IsApproved = false;
   Membership.UpdateUser(user);

 //  StringBuilder bodyMsg = new StringBuilder();

   Guid userID = (Guid)user.ProviderUserKey;


   msg.Subject = "Submission Confirmation";
   bodyMsg.Append("<html><head><img src=" + "http://waag.ir/images/header.jpg" + ">" + "<title>CONFIRMATION EMAIL:</title></head><body>");
   bodyMsg.Append("<br/>");
   string link = string.Format("http://www.waag.ir/Activate.aspx?userID={0}", userID.ToString());
   bodyMsg.Append("Dear " + RadioButtonList_Prefix.SelectedItem.Text + " " + name.Text + " " + middle.Text + " " + lastname.Text + ":<br> Thank you for registering with Avestia Publishing manuscript submission system. To confirm and complete your registration, please follow the link below:</br>" + link + "</br>This link is active for 48 hours. If the link is not visited within this time frame, your registration will be discarded and you will need to register again.</br></br></br>Best regards,</br>Avestia Publishing</br>http://avestia.com");

   msg.BodyEncoding = System.Text.Encoding.GetEncoding("utf-8");
   msg.Priority = MailPriority.High;

   msg.Body = bodyMsg.ToString();
   msg.IsBodyHtml = true;
   msg.BodyEncoding = System.Text.Encoding.GetEncoding("utf-8");
   msg.Priority = MailPriority.High;

   //  msg.ReplyTo = new MailAddress(TextBox2.Text);
   msg.From = new MailAddress("goldenstudio@goldenstudio.ir");
   msg.To.Add(new MailAddress(TextBox_email.Text));
   SmtpClient mailsender = new SmtpClient();

   mailsender.Host = "SmtpClient.goldenstudio.ir";

   mailsender.Port = 587;
   mailsender.EnableSsl = true;
   mailsender.Credentials = new System.Net.NetworkCredential("goldenstudio@goldenstudio.ir", "classaspnet");

   SmtpClient smtp = new SmtpClient();
   //Literal1.Text = "<script>alert(' ')</script>";
   smtp.Send(msg);
Was it helpful?

Solution 2

You need to record, in the database, when the email was sent and then compare that date with the date that the user clicks the link. So, for example, you send the email at 31/12/2012 18:22 and I click on the link on 01/01/2013 18:22 then that is only 1 day.

You will need to build logic in to either a stored procedure or your asp.net code that runs on the confirmation page that checks that DateSent field

Edit
You 'expire' the link by making it valid only before a specific date. If the user tries to use the link after the date then you treat it as expired and show a relevant message. But you need to store the date in the database, in my original suggestion I've said to store the date you send out the email (in a DateSent field) and compare against that, another answer suggests setting the date the link expires (in an ExpiryDate field). Either approach will work, they are just coming at the problem from different ends of the problem.

OTHER TIPS

Add a column in your Users table (I'm assuming you have one) called ConfirmationDueDate and set it to 48 hours from the time the user clicks OK to create the account. When the user clicks the link, if the current time is past the ConfirmationDueDate value, take them back to the account creation page.

You need to record, in the database, when the email was sent and then compare that date with the date that the user clicks the link. So, for example, you send the email at 31/12/2012 18:22 and I click on the link on 01/01/2013 18:22 then that is only 1 day.

You will need to build logic in to either a stored procedure or your asp.net code that runs on the confirmation page that checks that DateSent field

Edit You 'expire' the link by making it valid only before a specific date. If the user tries to use the link after the date then you treat it as expired and show a relevant message. But you need to store the date in the database, in my original suggestion I've said to store the date you send out the email (in a DateSent field) and compare against that, another answer suggests setting the date the link expires (in an ExpiryDate field). Either approach will work, they are just coming at the problem from different ends of the problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top