Login & Forget Password Via Email Address

Posted by Community Admin on 03-Aug-2018 14:44

Login & Forget Password Via Email Address

All Replies

Posted by Community Admin on 19-Jan-2012 00:00

Hi Everyone,

I am looking for a solution of validate my user via user name or email address, mainly for forget password because lots of user don't remember their user name in order to retrieve password.

Regards,
QainanIdris.com

Posted by Community Admin on 19-Jan-2012 00:00

Hi Qainan,

I created a user control and stuck it on a page to help my users retrieve their username via their email address. It asks for their email address and if its in the database, it sends their username to their email.  Not sure if this is the most elegant solution (as I'm just a beginner programmer), but it works for us.

cs

protected void btnSubmit_OnClick(object sender, EventArgs e)
    
        SqlConnection cnn = new SqlConnection(getSitefinity());
        String command = String.Format("Select u.email, u.[user_name] FROM [Sitefinity].[dbo].[sf_users] u where u.email = '0'", txtEmail.Text);
        SqlCommand cmd = new SqlCommand(command, cnn);
        SqlDataAdapter adapter = new SqlDataAdapter(cmd);
        DataSet Users = new DataSet();
        adapter.Fill(Users, "credentials");
 
        if (Users.Tables["credentials"].Rows.Count == 0)
        
            lblMessage.Visible = true;
            lblMessage.CssClass = "error";
            lblMessage.Text = "Email not in database. Please check your spelling. If you continue to have trouble, contact support@pa-news.org for assistance.";
        
 
        else
        
            foreach (DataRow u in Users.Tables["credentials"].Rows)
            
                string username = u["user_name"].ToString();
                string userEmail = u["email"].ToString();
                MailMessage message = new MailMessage();
 
                message.From = new MailAddress("Support@pa-news.org");
                message.To.Add(new MailAddress(userEmail));
                message.Subject = "Username for PNA Website";
                message.Body = String.Format("<p>You are receiving this email because you requested your username for the Pennsylvania Newspaper Association website. If you did not request this, please contact support@pa-news.org.</p><p>Your username is 0</p>", username);
                message.IsBodyHtml = true;
                SmtpClient client = new SmtpClient();
                client.Send(message);
                lblMessage.Visible = true;
                lblMessage.CssClass = "success";
                lblMessage.Text = String.Format("An email has been sent to 0 from Support@pa-news.org. Please check your junk/spam folders. If you cannot locate the email, please contact us at Support@pa-news.org.", txtEmail.Text);
            
ascx
<p>Please enter your email below. If you are in our system, you will receive an email with your username. You many need to allow (whitelist) emails from Support@pa-news.org in order to avoid email junk filters.</p>
 
    <asp:Label ID="lblMessage" Visible="false" runat="server" Text="Label"></asp:Label>
 
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_OnClick" />    
   <asp:RegularExpressionValidator ID="revEmail" runat="server"
                    ControlToValidate="txtEmail" CssClass="warning"
                    ErrorMessage="Invalid Email Format" ForeColor=" "
                    ValidationExpression="^[\n <]*([\'a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+)">Format: name@website.com</asp:RegularExpressionValidator>

Posted by Community Admin on 22-Jan-2012 00:00

Dear Amanda,

Thank you very much for your code. it's really help me a lot. I have done small modification in CS code in order to update user password once he/she is putting email address.

string username = u["user_name"].ToString();
string userEmail = u["email"].ToString();
Guid guid = new Guid(u["id"].ToString());
string userAnswer = u["password_answer"].ToString();

var userManager = UserManager.GetManager("Default");
var user = userManager.GetUsers().Where(y => y.Id == guid).Single();
string userPassword = userManager.ResetPassword(guid, userAnswer);
userManager.SaveChanges();

Once password is updated I send via email to registered user.

Thank you once again.

Regards,
QainanIdris.com

This thread is closed