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/22/2009 2:32:19 AM #

trackback

Retrieving the Password for a User in Asp.net

Thank you for submitting this cool story - Trackback from DotNetShoutout

DotNetShoutout

5/22/2009 3:05:48 AM #

trackback

Retrieving the Password for a User in Asp.net

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

DotNetKicks.com

5/22/2009 3:17:08 AM #

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/22/2009 7:40:56 PM #

pingback

Pingback from rapid-dev.net

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

rapid-dev.net

5/25/2009 8: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/25/2009 8: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/11/2009 12:25:44 PM #

Online Poker

Thanks for the code as well. Since I am new to ASP.net, this code is helping me out now.

Online Poker

6/22/2009 9:33:15 PM #

pingback

Pingback from answerspluto.com

list of urls - 5 « Answers Pluto

answerspluto.com

9/21/2009 4:05:24 AM #

SEO

I was trying to retrieve hashed passwords before reading Richard's point. Thanks to you both.

SEO United Kingdom

10/5/2009 11:09:45 AM #

Ontario name change

Is retrieving the password may affect the application or it is safe in hack?

Ontario name change United States

1/9/2010 7:43:16 AM #

Apartments Buenos Aires

This is an interesting topic Ashic. Thanks for comming up with it, now I understand a little more about it.

Apartments Buenos Aires United States

1/20/2010 12:49:45 PM #

bamboo floors

Hi,

Why didn’t I find this post earlier? Keep up the good work

bamboo floors United States

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