如何将AutoGeneratedPassword更新到数据库?

时间:2015-07-16 15:19:00

标签: c# asp.net

使用System;

使用System.Collections.Generic;

使用System.Linq;

使用System.Web;

使用System.Web.UI;

使用System.Web.UI.WebControls;

使用System.Data;

使用System.Data.SqlClient;

使用System.Net.Mail;

使用System.Net;

使用System.Configuration;

public partial class forgotpw:System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)
{

}

private string getRandomPassword()
{

    string allowedChars = "";

    allowedChars = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,";

    allowedChars += "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,";

    allowedChars += "1,2,3,4,5,6,7,8,9,0,!,@,#,$,%,&,?";

    char[] sep = { ',' };

    string[] arr = allowedChars.Split(sep);

    string PasswordString = "";

    string temp = "";

    Random rand = new Random();

    for (int i = 0; i < 8; i++)
    {

        temp = arr[rand.Next(0, arr.Length)];

        PasswordString += temp;

    }

    return PasswordString;

}


protected void btnGenerate_Click(object sender, EventArgs e)
{

    SmtpClient client = new SmtpClient("smtp.gmail.com");

    var message = new MailMessage();

    client.Host = "smtp.gmail.com";

    client.Port = 587;

    client.UseDefaultCredentials = false;

    client.Credentials = new System.Net.NetworkCredential("my@gmail.com", "mypw");

    client.EnableSsl = true;

    MailAddress SendFrom = new MailAddress("my@gmail.com", "mygmail");

    try
    {

        String Password = getRandomPassword();

        MailAddress SendTo = new MailAddress(txtEmail.Text);

        message = new MailMessage(SendFrom, SendTo);

        message.Subject = "Auto Generated Password";

        message.Body = "Auto Generated Password has been generated, login using below creditiantials:<br/>" + "<br/>LoginID :" + txtEmail.Text + "<br/>Password :" + Password;

        message.IsBodyHtml = true;

        message.Priority = MailPriority.High;

        client.ServicePoint.MaxIdleTime = 0;

        client.ServicePoint.ConnectionLimit = 1;

        client.Send(message);

        ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE='javascript'>alert('Mail Sent successfully');</script>");

    }

    catch
    {

        ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE='javascript'>alert('Unexpected Error Occur TryAgain ');</script>");

    }

}

自动生成的密码已发送至电子邮件但未在数据库中更新...请帮助..谢谢

1 个答案:

答案 0 :(得分:0)

您必须以任何方式将新生成的密码保存到数据库,而不是访问数据库。在发送电子邮件之前,请检查用于保存数据和更新用户信息的方法。我建议向用户发送重置密码的链接,不要公开新密码,因为它更安全。

Nelson Parra。