用户注册中激活邮件的时间限制 [英] Timing limit in Activation mail in User registration

查看:97
本文介绍了用户注册中激活邮件的时间限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经完成了用户注册的激活链接..它工作正常..

i需要为该特定链接添加时间。

例如:Aft 20分钟,如果用户未在特定时间内激活其帐户,则应停用链接并在注册时删除值

...





protected void Button1_Click(object sender,EventArgs e)

{

int userId = 0;

string constr = ConfigurationManager.ConnectionStrings [mlaConnectionString]。ConnectionString;

using(SqlConnection con = new SqlConnection(constr))

{

使用(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 =用户名已经存在。\\ n请选择其他用户名。;

休息;

案例-2:

message =提供的电子邮件地址已被使用。;

休息;

默认:

message =注册成功。 \ \\ n用户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))

{

使用(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();

使用(MailMessage mm = new MailMessage(sender@gmail.com,txtemailid.Text)) />
{

mm.Subject =帐户激活;

string body =Hello+ txtusername.Text.Trim()+, ;

body + =

请点击以下链接激活您的帐户;

body + =
点击此处激活您的帐户。;

body + =

谢谢;

mm.Body = body;

mm.IsBodyHtml = tr ue;

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);

}

}







我需要修改的是什么做这个编码....





提前感谢

解决方案

< blockquote>当您将记录插入UserActivation表时,也插入日期时间。



当用户点击激活链接时,可以比较当前时间和激活令牌生成时间。如果时差超过20分钟,则可以显示激活时间已过期的消息。如果用户点击20分钟就可以激活帐户。


使用sql的datediff函数:



选择DATEDIFF(分钟,开始日期,结束日期)







//这里startdate将是日期时间用户已注册& enddate将是用户点击链接时的日期时间。


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

解决方案

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.


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.


这篇关于用户注册中激活邮件的时间限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆