Password Reset for membership in Asp.net

Source: Internet
Author: User
Password Reset for membership in Asp.net

Author: Yi Mingzhi

Date: Wednesday, June 13,200 7

The membership mechanism introduced in Asp.net 2.0 brings us a lot of convenience for web development, which reduces the workload when dealing with users, roles, and permissions in many project development projects. However, we may encounter a small problem during development:

We know that membershipuser has the following overload methods:

  • Membershipuser. resetpassword (): reset the user password to an automatically generated new password.
  • Membershipuser. resetpassword (string passwordanswer): reset the user password to an automatically generated new password.

The following descriptions are available in msdn:

ResetpasswordCall the membershipprovider. resetpassword method of the membership provider referenced by the providername property to reset the password of the membership user to a new automatically generated password. Then, return the new password to the caller.

If enablepasswordreset isFalseThe membership provider returns an exception.

If requiresquestionandanswer isTrue, You must use the resetpassword overload method with the password prompt answer as the parameter, and provide the password of the qualified user. If you need a password and provide an incorrect password, the membership provider will trigger membershippasswordexception.

Another method is membershipuser. changepassword (string oldpassword, string newpassword) must provide the original password, so here there is a conflict: we hope that users can use Security Questions and Answers to retrieve the password, what should I do if I want the Administrator to reset the password of a user? None of the above methods can be used directly, or use membershipuser. resetpassword () without security questions and answer verification, or you must know the answer or the original password. We know that security-related things are encrypted and often adopt one-way encoding, that is to say, it is impossible for us to easily know the user's answer and old password by reading the database data! Well, how can we solve this problem? Here is a small solution:

The membership database contains a stored procedure for setting the user password:


 
 
  1. CREATE PROCEDURE aspnet_Membership_SetPassword
  2. (
  3.  
  4.  @ApplicationName nvarchar(256),
  5.  
  6.  @UserName nvarchar(256),
  7.  
  8.  @NewPassword nvarchar(128),
  9.  
  10.  @PasswordSalt nvarchar(128),
  11.  
  12.  @CurrentTimeUtc datetime,
  13.  
  14.  @PasswordFormat int = 0
  15. )

Return Value: 0 is returned for success;
If the user does not exist, 1 is returned;

So OK, let's start solving our needs! The idea is simple: first set a user's default password, and then use membershipuser. changepassword (string oldpassword, string newpassword) as the old password to change the user's password. The following code is written on the premise that the dataaccess. runprocedure method has been implemented to execute the stored procedure:


 
 
  1. Public ClassMembershipsafe
  2. {
  3. Public Static StringResetpassword (StringUsername)
  4. {
  5. // Change the user password to an 8-bit random password that contains a special symbol
  6. ReturnResetpassword (username, membership. generatepassword (8, 1 ));
  7. }
  8. Public Static StringResetpassword (StringUsername,StringNewpassword)
  9. {
  10. // First change the user password to 123123
  11. Sqlparameter [] _ sp = {
  12. NewSqlparameter ("@ applicationname", membership. applicationname)
  13. ,NewSqlparameter ("@ username", username)
  14. ,NewSqlparameter ("@ newpassword", "1m4h3ezlakw1wbvttwyjijza33w = ")
  15. ,NewSqlparameter ("@ passwordsalt", "rcvy3pcccz9txw7nhp1maw = ")
  16. ,NewSqlparameter ("@ currenttimeutc", datetime. Now)
  17. ,NewSqlparameter ("@ passwordformat", 1)
  18. };
  19. BoolOP = dataaccess. runprocedure ("aspnet_membership_setpassword", _ Sp) = 0;
  20. // Use 123123 as the original password and change it to the new password
  21. Membership. getuser (username). changepassword ("123123", newpassword );
  22. ReturnNewpassword;
  23. }
  24. }

Note:The above code is for reference only. It does not necessarily meet the requirements of your specific project. For example, someone may continue to ask how to tell the user to change the password. The answer is email or phone notification, haha ~~

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.