Click here to Skip to main content
15,867,939 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my asp.net project, in ForgotPassword page I am sending a link with unique id to the users emailid and also i am inserting email, uid, time in a forgotpassword table in sql server and that link refers users to a reset password page. Now I want to set expiration time of that link means I want that link will be expired in 1 day. How can I do that?
Posted
Comments
Siddhesh Patankar 5-Jul-13 4:52am    
What are you storing in the time field of DB Expiration Time or Creation tome
Gaurav Agarwal from Jaipur 5-Jul-13 4:53am    
Creation time sir..!
Siddhesh Patankar 5-Jul-13 5:01am    
Please dont call me Sir :)

You have not provided any code for me to see how you have tried but still
Try this

<pre lang="c#">

DateTime dtCreate= [Get Date from DB];
DateTime dtNow = DateTime.Now;
DateTime dtExp = dtCreate.AddDays(1);
if(dtNow > dtExp)
{
Response.Write("Page has Expired!!!!");
}


</pre>
 
Share this answer
 
Comments
Gaurav Agarwal from Jaipur 5-Jul-13 5:28am    
how can i get date from db?
Gaurav Agarwal from Jaipur 5-Jul-13 5:33am    
protected void btnSubmit_Click(object sender, EventArgs e)
{
string guid = g.ToString();
DateTime d = DateTime.Now;
string s = "insert into tblForgotPassword(Email, ActivationCode, TimeDuration,Status) values('"+txtemail.Text+"','"+guid+"','"+d.ToString()+"','Pending')";
da1 = new SqlDataAdapter(s,con);
ds1 = new DataSet();
da1.Fill(ds1);


And this is email body

string Body = "Hi, this mail is to change your password" +
"To Change your Password Please click below<br/>";
Body += "http://localhost:1354/socialnetworksite/ResetPassword.aspx?uid=" + g.ToString();

Msg.Body = Body;
Here is my code:


public partial class ForgotPassword : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ToString());
SqlDataAdapter da,da1;
SqlCommand cmd;
DataSet ds,ds1;
Guid g = Guid.NewGuid();
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnSubmit_Click(object sender, EventArgs e)
{
string guid = g.ToString();
DateTime d = DateTime.Now;
//Response.Write(d.ToString());

string s = "insert into tblForgotPassword(Email, ActivationCode, TimeDuration,Status) values('"+txtemail.Text+"','"+guid+"','"+d.ToString()+"','Pending')";
da1 = new SqlDataAdapter(s,con);
ds1 = new DataSet();
da1.Fill(ds1);

try
{
DataSet ds = new DataSet();

{
con.Open();
SqlCommand cmd = new SqlCommand("SELECT RegId,UserName,Password FROM tblRegistration Where Email= '" + txtemail.Text.Trim() + "'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
con.Close();
Session["email"] = txtemail.Text;
}



if (ds.Tables[0].Rows.Count > 0)
{
Session["username"] = ds.Tables[0].Rows[0]["UserName"];
Session["RegId"] = ds.Tables[0].Rows[0]["RegId"];
MailMessage Msg = new MailMessage();
// Sender e-mail address.
Msg.From = new MailAddress(txtemail.Text);
// Recipient e-mail address.
Msg.To.Add(txtemail.Text);
Msg.Subject = "Your Password Details";
//Msg.Body = "Hi,
Please check your Login Detailss

Your Username: " + ds.Tables[0].Rows[0]["UserName"] + "

Your Password: " + ds.Tables[0].Rows[0]["Password"] + "

";

string Body = "Hi, this mail is to change your password" +
"To Change your Password Please click below
";
Body += "http://localhost:1354/socialnetworksite/ResetPassword.aspx?uid=" + g.ToString();

Msg.Body = Body;

Msg.IsBodyHtml = true;
// your remote SMTP server IP.
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential("abc@gmail.com", "abc");
smtp.EnableSsl = true;
smtp.Send(Msg);
//Msg = null;
lbltxt.Text = "Your Password Details Sent to your mail";
// Clear the textbox valuess
txtemail.Text = "";
}
else
{
lbltxt.Text = "The Email you entered not exists.";
}
}
catch (Exception ex)
{
Console.WriteLine("{0} Exception caught.", ex);
}
}
public string Encryptdata(string password)
{
string strmsg = string.Empty;
byte[] encode = new byte[password.Length];
encode = Encoding.UTF8.GetBytes(password);
strmsg = Convert.ToBase64String(encode);
return strmsg;
}
}
 
Share this answer
 
v2
Comments
Siddhesh Patankar 5-Jul-13 5:32am    
The code needs to be entered in the ResetPassword.aspx form. The Logic will be simple when user clicks the link using the code I have given you, you can check if the link has expired or not.
Put master 2 div tags in the ResetPassowrd.aspx.

e.g.
<div ID="Valid" runat="server">
--Put your valid data here
</div>
<div ID="Expired" runat="server">
Put Expired Message here
</Div>

The modifcation to my solution in code behind(write in Page_lOad Event)
DateTime dtCreate= [Get Date from DB];
DateTime dtNow = DateTime.Now;
DateTime dtExp = dtCreate.AddDays(1);
if(dtNow > dtExp)
{
Expired.Visible=true;
Valid.Visible=false;
}
else
{
Expired.Visible=false;
Valid.Visible=true;
}
Siddhesh Patankar 5-Jul-13 5:33am    
and please delete the line in your code where you have provided the credentials
Gaurav Agarwal from Jaipur 5-Jul-13 5:36am    
Thank you very much Mr Patankar!

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