0
votes

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data;

using System.Data.SqlClient;

using System.Net.Mail;

using System.Net;

using 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("[email protected]", "mypw");

    client.EnableSsl = true;

    MailAddress SendFrom = new MailAddress("[email protected]", "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>");

    }

}

The Auto Generated Password has been sent to email but is not updated on database ... Please help..thank you

1

1 Answers

0
votes

You have to save the new generated password in any way to your database, you are not accessing your database. Check the methods you are using to save the data and update user info before sending the email. I suggest to send to the user a link to reset the password and not exposing the new password as it's more secure.

Nelson Parra.