Example #1
0
        protected void btnForgotPassword_Click(object sender, EventArgs e)
        {
            string          email = txtUserEmail.Text;
            DataAccessLayer dao   = new DataAccessLayer();

            if (dao.CheckUserExists(email) == true)
            {
                var chars       = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
                var stringChars = new char[50];
                var random      = new Random();

                for (int i = 0; i < stringChars.Length; i++)
                {
                    stringChars[i] = chars[random.Next(chars.Length)];
                }

                string finalString = new String(stringChars);

                if (dao.InsertPasswordResetLink(email, finalString) == true)
                {
                    MailAddress to   = new MailAddress(email);
                    MailAddress from = new MailAddress(Constants.emailID);

                    string baseUrl  = Request.Url.GetLeftPart(UriPartial.Authority);
                    string ResetUrl = baseUrl + "/ResetPassword.aspx?resetID=" + finalString + "&userID=" + email;

                    string encodedResetUrl = new Uri(ResetUrl).AbsoluteUri;

                    string link = "<a href='" + encodedResetUrl + "'>" + encodedResetUrl + "</a>";

                    MailMessage message = new MailMessage(from, to);
                    message.Subject = "Password Reset - Kent Homes";
                    message.Body    = "<p>Click the below to reset the password<p>" + link;

                    message.IsBodyHtml = true;

                    SmtpClient client = new SmtpClient(Constants.serverAddress, Constants.emailPort)
                    {
                        Credentials = new NetworkCredential(Constants.emailID, Constants.emailPassword),
                        EnableSsl   = true
                    };
                    // code in brackets above needed if authentication required

                    try
                    {
                        client.Send(message);
                    }
                    catch (SmtpException ex)
                    {
                        Console.WriteLine(ex.ToString());
                    }
                    responseForgotPassword.Text = "Password Reset Link sent to Email";
                }
            }
            else
            {
                responseForgotPassword.Text = "UserID doesn't Exists!";
            }
        }
Example #2
0
        protected void register_Click(object sender, EventArgs e)
        {
            string name            = txtName.Text;
            string email           = txtEmail.Text;
            string pass            = txtPassword.Text;
            string confirmPass     = txtConfirmPassword.Text;
            string responseMessage = null;

            DataAccessLayer dao = new DataAccessLayer();

            byte[] bytePassword = System.Text.ASCIIEncoding.ASCII.GetBytes(pass);
            System.Security.Cryptography.HashAlgorithm hashAlgorithm;

            if (email.Length % 3 == 0)
            {
                hashAlgorithm = SHA256.Create();
            }
            else if (email.Length % 3 == 1)
            {
                hashAlgorithm = SHA512.Create();
            }
            else
            {
                hashAlgorithm = SHA1.Create();
            }

            byte[] byteHashPassword  = hashAlgorithm.ComputeHash(bytePassword);
            string encryptedPassword = Convert.ToBase64String(byteHashPassword);

            if (dao.CheckUserExists(name, email, encryptedPassword) == false)
            {
                responseMessage           = "Username already exists";
                responseRegistration.Text = responseMessage;
            }
            else
            {
                responseMessage           = "Registration successful";
                responseRegistration.Text = responseMessage;
            }
        }