如何设置链接的到期时间? [英] How to set Expiration time to the link?
本文介绍了如何设置链接的到期时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的asp.net项目中,在ForgotPassword页面中,我向用户emailid发送一个具有唯一ID的链接,并且我在sql server中的忘记密码表中插入电子邮件,uid,time,该链接将用户重置为重置密码页面。现在我想设置该链接的到期时间意味着我希望该链接在1天内过期。我怎么能这样做?
解决方案
你还没有为我提供任何代码,看看你怎么试过但仍然是
试试这个
< pre lang =c#>
DateTime dtCreate = [从DB获取日期] ;
DateTime dtNow = DateTime.Now;
DateTime dtExp = dtCreate.AddDays(1);
if(dtNow> dtExp)
{
Response.Write(页面已过期!!!!);
}
< / pre>
这是我的代码:
公共部分类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 =插入tblForgotPassword(电子邮件,ActivationCode,TimeDuration,Status)值('+ txtemail.Text +','+ guid + ','+ d.ToString()+','待定');
da1 =新的SqlDataAdapter(s,con);
ds1 = new DataSet();
da1.Fill(ds1);
试试
{
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(b) ds.Tables [0] .Rows.Count> 0)
{
会话[用户名] = ds.Tables [0] .Rows [0] [UserName];
Session [RegId] = ds.Tables [0] .Rows [0] [RegId];
MailMessage Msg = new MailMessage();
/ /发件人电子邮件地址。
Msg.From =新邮件地址(txtemail.Text);
//收件人电子邮件地址。
Msg.To.Add(txtemail.Text);
Msg.Subject =您的密码详情;
//Msg.Body =
请检查您的登录详细信息
您的用户名:+ ds.Tables [0] .Rows [0] [UserName] +
您的密码:+ ds.Tables [0 ] .Rows [0] [密码] +
;
string Body =这封邮件是为了更改你的密码+
更改密码请点击下方
;
Body + =http:// loc alhost:1354 / socialnetworksite / ResetPassword.aspx?uid =+ g.ToString();
Msg.Body = Body;
Msg.IsBodyHtml = true;
//您的远程SMTP服务器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 =您的密码详细信息发送到您的邮箱;
//清除文本框值
txtemail .Text =;
}
其他
{
lbltxt.Text =您输入的电子邮件不存在。;
}
}
catch(例外情况)
{
Console.WriteLine({0}捕获异常。,ex);
}
}
公共字符串Encryptdata(字符串密码)
{
string strmsg = string.Empty;
byte [] encode = new byte [password.Length];
encode = Encoding.UTF8。 GetBytes(密码);
strmsg = Convert.ToBase64String(encode);
返回strmsg;
}
}
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?
解决方案
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>
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;
}
}
这篇关于如何设置链接的到期时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文