views:

798

answers:

3

Hello,

I am so close to get this project done. I need to retrieve the password and passwordSalt from my Membership table to compare it to my 'OldPasswords' table.

The problem is the Membership provider does not let me use the GetPassword method because the password is hashed.

And I can not retrieve it in a normal sqlConnection because the UserID is hashed also.

Does anyone know how to hash the UserID so I can put it in my where clause?

Or maybe there is a different way to get to that data?

Any help is appreciated.

Thank you,

Steve

+2  A: 

There seem to be a couple of different things going on here...

  • You cannot recover a hashed password. Period. The purpose of hashing is to prevent exactly this kind of recovery.

  • You can hash the User ID for a lookup if the User ID value is already hashed in the database for some reason (although, that is a little strange, there is no good reason to hash a User ID). But you need to know how it was hashed. If it's MD5 or SHA1, the quickest way is to use FormsAuthentication.HashPasswordForStoringInConfigFile (but use it on the user name instead of the password).

  • The salt should definitely not be hashed, otherwise it's unusable. Salts are appended to the clear-text password before hashing, so whatever value you see in the salt column is the salt.

Aaronaught
Yes, with previous help I was able to finally get the correct hash together to has the password and compare it to my table, BUT now I notice that the username is also scrambled, and using the same hashing function did not work to get a match.
Steve
@steve - the username should not be getting mangled. Something hinky is going on.
Sky Sanders
+2  A: 

Steve, the UserId is not hashed. You may be confusing UserName with UserId (ProviderUserKey) which is a Guid.

In the context of your other questions: You should reference this code in both the code that you use to create a new user in order to log the initial password hash, salt and format AND in the OnPasswordChanging so that you can check/reject/insert.

This will get the relevant information for the currently logged in user:

var user = Membership.GetUser();
var userId = user.ProviderUserKey;

MembershipPasswordFormat passwordFormat;
string passwordSalt;
string password;

var cstring = WebConfigurationManager.ConnectionStrings["localSqlServer"];
using (var conn = new SqlConnection(cstring.ConnectionString))
{
    using (var cmd = conn.CreateCommand())
    {
        cmd.CommandText = "select PasswordFormat,PasswordSalt,Password from aspnet_Membership where UserId=@UserId";
        cmd.Parameters.AddWithValue("@UserId", userId);
        conn.Open();
        using (var rdr = cmd.ExecuteReader())
        {
            if (rdr != null && rdr.Read())
            {
                passwordFormat = (MembershipPasswordFormat) rdr.GetInt32(0);
                passwordSalt = rdr.GetString(1);
                password = rdr.GetString(2);
            }
            else
            {
                throw new Exception("An unhandled exception of type 'DoesntWorkException' has occured");
            }
        }
    }
}

//do something interesting hew with passwordFormat, passwordSalt , password 
Sky Sanders
You code worked great, thank you very much for your help. Once I am completely done I will post all my code. Maybe others find it helpful.
Steve
@Steve - hmm..... a shiny gold check mark next to this and my other answers would help people zero in on the correct answer, eh? ;-)
Sky Sanders
Yes of course, sorry, I am still new around here and how this works. Very cool.
Steve
A: 

@Sky, here are all my columns.

MemberShip Columns

Steve
Don't post additional information as "answers". You are just confusing the thread. Edit your question to provide additional information.
Mystere Man
How do I post images in comments?
Steve