Advertisements

How to regenerate a new password using C# and Asp.net


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;

public partial class _Default : 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("Your@gmail.com", 
"yourpassword");

client.EnableSsl = true;

MailAddress SendFrom = new MailAddress("Youremail@gmail.com", "Hai");

try
{

String password = getRandomPassword();

MailAddress SendTo = new MailAddress(txtEmail.Text);

message = new MailMessage(SendFrom, SendTo);

message.Subject = "Auto Generated Password";

message.Body = "Your Account has ben hacked...!! Auto Generated Password 
has been generated, login using below creditiantials:
" + "
UserName :" + 
txtEmail.Text + "
Password :" + password;

message.IsBodyHtml = true;

message.Priority = MailPriority.High;

client.ServicePoint.MaxIdleTime = 0;

client.ServicePoint.ConnectionLimit = 1;

client.Send(message);

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

}

catch
{

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

}

}
}


Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: