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…
  • Facebook
  • DotNetKicks
  • Digg
  • LinkedIn
  • Technorati
  • del.icio.us
  • Google
  • Live
  • Tumblr
  • msdn Social
  • Ping.fm
  • Reddit
  • Slashdot
  • StumbleUpon
  • TwitThis
Categories: ASP.NET

Comments

4/18/2009 1:32:19 PM #

trackback

Retrieving the Password for a User in Asp.net

Thank you for submitting this cool story - Trackback from DotNetShoutout

DotNetShoutout

4/18/2009 2: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

4/18/2009 2: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

4/19/2009 6:40:56 AM #

pingback

Pingback from rapid-dev.net

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

rapid-dev.net

4/22/2009 7:25:29 AM #

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

4/22/2009 7:28:57 AM #

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

5/20/2009 8:33:15 AM #

pingback

Pingback from answerspluto.com

list of urls - 5 « Answers Pluto

answerspluto.com

5/16/2010 8:05:09 PM #

pingback

Pingback from 223.tvshowzone.com

Pontiac Trans Sport Part Accessories, Part Guide 2008 Pontiac G8

223.tvshowzone.com

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.6.1.0
Theme by Ashic Mahtab

Need an expert?

Ashic Mahtab
ashic@live.com
(+44) 07879927393

Stats

Featured Ads

 

Donations

I maintain this site and create all content entirely in my own time just to help you guys out. If you find the stuff helpful, or cool or just like what you see, I'd appreciate you chipping in to help out with the hosting costs. It's easy to do so - just click the button below - no amount is too low :)