Guest User

Guest User

  • Tech Writer
  • 271
  • 36.2k

change password in asp.net using Entity Framework

Oct 15 2020 6:42 AM
i want when user wants to change password its first enter existing password then new password...
  1. <table class="style1">  
  2. <tr>  
  3. <td>Current Password:</td>  
  4. <td>  
  5. <asp:TextBox ID="TxtCrntPswd" runat="server"></asp:TextBox>  
  6. </td>  
  7. </tr>  
  8. <tr>  
  9. <td>New Password:</td>  
  10. <td>  
  11. <asp:TextBox ID="TxtNwPswd" runat="server"></asp:TextBox>  
  12. </td>  
  13. </tr>  
  14. <tr>  
  15. <td>Confirm New Password:</td>  
  16. <td>  
  17. <asp:TextBox ID="TxtCnPswd" runat="server"></asp:TextBox>  
  18. </td>  
  19. <td>  
  20. <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"  
  21. ControlToValidate="TxtCnPswd" ErrorMessage="!!" ForeColor="Red"  
  22. SetFocusOnError="True"></asp:RequiredFieldValidator>  
  23. <asp:CompareValidator ID="CompareValidator1" runat="server"  
  24. ControlToCompare="TxtNwPswd" ControlToValidate="TxtCnPswd"  
  25. ErrorMessage="The new password and confirm password not same !!" ForeColor="Red"></asp:CompareValidator>  
  26. </td>  
  27. </tr>  
  28. </table>  
  29. </div>  
  30. <asp:Button ID="BtnPswd" runat="server" Text="Save" OnClick="BtnPswd_Click" />  
  1. protected void BtnPswd_Click(object sender, EventArgs e)  
  2. {  
  3. using (var context = new schoolEntities6())  
  4. {  
  5. string strmsg = string.Empty;  
  6. byte[] encode = new byte[TxtCnPswd.Text.ToString().Length];  
  7. encode = Encoding.UTF8.GetBytes(TxtCnPswd.Text);  
  8. strmsg = Convert.ToBase64String(encode);  
  9. encrypwd = strmsg;  
  10. var currPass = TxtCrntPswd.Text;  
  11. var newPass = TxtNwPswd.Text;  
  12. var rePass = strmsg;  
  13. User userEntity = (from x in context.Users  
  14. where  
  15. x.Password == TxtCrntPswd.Text  
  16. select x).FirstOrDefault();  
  17. //if (userEntity != null)  
  18. {  
  19. userEntity.Password = TxtCnPswd.Text;  
  20. context.SaveChanges();  
  21. }  
  22. else  
  23. {  
  24. lbl_msg.Text = "current password is incorrect";  
  25. }  
  26. }  
This scenario isn't working

Answers (1)