Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Pasword Reset Link in .NET

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 274
    Comment on it

    While resetting password of users you need to email link to the registered user for changing their passwords

     

    For doing that you have to perform the following line of codes

     

    /*    To send mail function is being created   /*

        public static bool SendMail()
                {
    

    /*    SMTP class is used to send mail   /*

     
                    SmtpClient smtpClient;
                    MailMessage message;
                    try
                    {
                        string senderEmailID = "hjhimanshubscit@gmail.com";
                        string senderPassword = demo@123";
                        string senderAddress = "Test@gmail.com";
                        string displayName = "Reset Link";
    
                        smtpClient = new SmtpClient();
                        message = new MailMessage();
    
                        smtpClient.Host = "smtp.gmail.com";
                        smtpClient.Port = 587;
    
                        message.IsBodyHtml = true;
                        smtpClient.UseDefaultCredentials = false;
                        smtpClient.Credentials = new NetworkCredential(senderEmailID, senderPassword);
    
                        smtpClient.EnableSsl = true;
    
                        message.From = new MailAddress(senderAddress, displayName);
    
                  string resetUrl = String.Format("{0}?id={1}&pswd={2}&exp={3}",
     "http://localhost:64343/PasswordReset.aspx", Encryption.Encrypt(Convert.ToString(person.id)), Encryption.Encrypt(person.password), Encryption.Encrypt(Convert.ToString(DateTime.Now)));
    
    
    

    /*    Set all the details related to the mail in your app.config  /*

     
                       message.Body = "Dear " + person.username + ",<br/> Request has been initiated to reset your password on the User Registeration application. Please <a href=" + resetUrl + " target='_blank'>click here </a> to reset your password. <br/><br/> " + ConfigurationManager.AppSettings["ExpireInString"];
                        message.Subject = "Reset Link";
                        message.To.Add(person.email);
                        smtpClient.Send(message);
    
                        return true;
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
            }
    

    In this Encypt is a function i have used for encrypting my data and will recieve it in query string by decrypting it again

      public static string Encrypt(string clearText)
        {
            byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
            using (Aes encryptor = Aes.Create())
            {
                Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
                encryptor.Key = pdb.GetBytes(32);
                encryptor.IV = pdb.GetBytes(16);
                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(clearBytes, 0, clearBytes.Length);
                        cs.Close();
                    }
                    clearText = Convert.ToBase64String(ms.ToArray());
                }
            }
            return clearText;
        }
    

     

     

    In this function we first create the object of the SMTP class in which the credentials and the message to be sent is passed and then the message to be sent is encrypted and attached to the mail function.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: