ASP.NET Snippet: Quick Password Reset
Here is a simple way, via code to reset a password when you are using the built-in ASP.NET authentication system. Useful if you either don't have a reset password form, or you just want to quickly change a password. Create a blank page, and place in the code behind Page_Load
event.
C#
protected void Page_Load(object sender, EventArgs e) { MembershipUser u = Membership.FindUsersByName("Username")["Username"]; u.UnlockUser(); u.ChangePassword(u.ResetPassword(), "newpassword"); }
VB
Public Sub Page_Load(sender As Object, e As EventArgs) Dim u As MembershipUser = Membership.FindUsersByName("Username")("Username") u.UnlockUser() u.ChangePassword(u.ResetPassword(), "newpassword") End Sub
Just delete the page when done.
Update (8 July 2009): If a question and answer is required when you create a user, you have to pass on the answer to u.ResetPassword
, e.g. u.ChangePassword(u.ResetPassword("answer"), "newpassword")
, otherwise ResetPassword
won't work.
Comments
Value cannot be null.
Parameter name: passwordAnswer
Thanks