Generate new password and send to user, if user is registered.
- Two
TextBox
on page,txtUserName
&txtEamil
. - User can provide either
UserName
orEmail
to get new password. - If
UserName
is provided then find User onusername
, if exists then generate new password and Email. - If user provided email then check if any user exists with provided email then generate new password and Email.
protected void btnSubmit_Click(object sender, EventArgs e)
{
string userName = txtUserName.Text.Trim();
string email = txtEmail.Text.Trim();
//
if (string.IsNullOrEmpty(userName) && string.IsNullOrEmpty(email))
{
lblMessage.Text = "Please provide at least either of the one, User Name or Email.";
}
else
{
SendNewPasswordToUser(userName, email);
}
}
private void SendNewPasswordToUser(string userName, string email)
{
if (!string.IsNullOrEmpty(userName))
{
MembershipUser mu = Membership.GetUser(userName);
if (mu != null)
{
string password = mu.ResetPassword();
EmailPassword(password, mu.Email);
}
}
else if (!string.IsNullOrEmpty(email))
{
userName = Membership.GetUserNameByEmail(email);
if (!string.IsNullOrEmpty(userName))
{
MembershipUser mu = Membership.GetUser(userName);
string password = mu.ResetPassword();
EmailPassword(password, mu.Email);
}
}
}
private void EmailPassword(string password, string toEmail)
{
string mailBody = string.Format("Your new password is {0}", password);
Mail.SendMail("[email protected]", toEmail, "New Password", mailBody);
}
Is there a better approach to avoid so many if
s?