Retrieving the Password for a User in Asp.net

by ashic 13. June 2009 19:30

If you’re using the Membership API in asp.net and need to retrieve a user’s password, you can do so by doing this:

MembershipUser user = Membership.GetUser("username");
string password = user.GetPassword();
string saferPassword = user.GetPassword("password answer");

The latter is safer as it requires you to pass in the user’s security answer as an added check. This will give you the unencrypted password [The default membership system stores hashed passwords in the database].

To support this feature, you’ll need to have password retrieval enabled in the web.config. You can do this in the <membership> node under <system.web>. It’ll look something like this:


<membership defaultProvider="myProvider">
      <providers>
        <add connectionStringName="LocalSqlServer" enablePasswordRetrieval="true"
          enablePasswordReset="true" requiresQuestionAndAnswer="true"
          applicationName="/" requiresUniqueEmail="false" passwordFormat="Encrypted"
          maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6"
          minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
          passwordStrengthRegularExpression="" name="myProvider"
          type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
     </providers>
</membership>

Hope that helps.

As Richard points out, hashed passwords cannot be retrieved. The hash is one way while having the password format set to encrypted enables retrieval of passwords. I’ve updated the web.config code to ensure that passwords can be retrieved.

Shout it
Share or Bookmark this post…
  • DotNetKicks
  • LinkedIn
  • Technorati
  • msdn Social
  • del.icio.us
  • Digg
  • Reddit
  • StumbleUpon
  • Slashdot
  • Live
  • Google
  • Facebook
Categories: ASP.NET

Comments

5/13/2009 6:32:19 PM #

trackback

Retrieving the Password for a User in Asp.net

Thank you for submitting this cool story - Trackback from DotNetShoutout

DotNetShoutout

5/13/2009 7:05:48 PM #

trackback

Retrieving the Password for a User in Asp.net

You've been kicked (a good thing) - Trackback from DotNetKicks.com

DotNetKicks.com

5/13/2009 7:17:08 PM #

trackback

Retrieving the Password for a User in Asp.net

If you’re using the Membership API in asp.net and need to retrieve a user’s password, you can do so by

Community Blogs

5/14/2009 11:40:56 AM #

pingback

Pingback from rapid-dev.net

Retrieving the Password for a User in Asp.net | rapid-DEV.net

rapid-dev.net

5/17/2009 12:25:29 PM #

Richard

You cannot retrieve a hashed password, you can only reset it. If you try to retrieve it, you'll get a ProviderException with the message "Hashed passwords cannot be decoded."

To enable password retrieval, you must also set the passwordFormat to "Encrypted".

Richard United Kingdom

5/17/2009 12:28:57 PM #

ashic

Thanks Richard. I'd actually copy / pasted and edited the web.config settings from another project of mine and set the enable retrieval to true. I should have also changed the password format to encrypted. Updating the post.

ashic Bangladesh

6/14/2009 1:33:15 PM #

pingback

Pingback from answerspluto.com

list of urls - 5 « Answers Pluto

answerspluto.com

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading




Powered by BlogEngine.NET 1.5.1.29
Theme by Ashic Mahtab

Stats