Click here to Skip to main content
15,937,779 members
Please Sign up or sign in to vote.
4.20/5 (2 votes)
See more:
I have done activation link for user registration.. it works fine..
i need to add timing for that particular link.
eg: Aft 20 mins, the link should be deactivated and the values inserted while registration also be deleted
if the user doesn't activated his account within that particular time...


protected void Button1_Click(object sender, EventArgs e)
{
int userId = 0;
string constr = ConfigurationManager.ConnectionStrings["mlaConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("Insert_User"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@username", txtusername.Text.Trim());
cmd.Parameters.AddWithValue("@password", txtpassword.Text.Trim());
cmd.Parameters.AddWithValue("@email", txtemailid.Text.Trim());
cmd.Connection = con;
con.Open();
userId = Convert.ToInt32(cmd.ExecuteScalar());
con.Close();
}
}
string message = string.Empty;
switch (userId)
{
case -1:
message = "Username already exists.\\nPlease choose a different username.";
break;
case -2:
message = "Supplied email address has already been used.";
break;
default:
message = "Registration successful.\\nUser Id: " + userId.ToString();
SendActivationEmail(userId);
break;
}
ClientScript.RegisterStartupScript(GetType(), "alert", "alert('" + message + "');", true);
}
}

private void SendActivationEmail(int userId)
{
string constr = ConfigurationManager.ConnectionStrings["mlaConnectionString"].ConnectionString;
string activationCode = Guid.NewGuid().ToString();
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO UserActivation VALUES(@UserId, @ActivationCode)"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@UserId", userId);
cmd.Parameters.AddWithValue("@ActivationCode", activationCode);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
using (MailMessage mm = new MailMessage("sender@gmail.com", txtemailid.Text))
{
mm.Subject = "Account Activation";
string body = "Hello " + txtusername.Text.Trim() + ",";
body += "

Please click the following link to activate your account";
body += "
Click here to activate your account.";
body += "

Thanks";
mm.Body = body;
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
NetworkCredential NetworkCred = new NetworkCredential("priyadharshan91@gmail.com", "*******");
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mm);
}
}



what are all the modification i have to do in this coding....


thanks in advance
Posted
Comments
Jignesh Khant 28-Apr-14 0:38am    
You can use datediff function of sql for each record.

when you insert record to UserActivation table insert the date time as well.

when user click on activation link, you can compare current time and activation token generated time. if the time difference more than 20 minutes you can show the message with activation time expired. if user click withing 20 minutes you can activate the account.
 
Share this answer
 
Use datediff function of sql:

Select DATEDIFF(minute,startdate,enddate)



//here startdate will be the datetime when user was registered & enddate will be the datetime when user clicks the link.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900