user1108948
user1108948

Reputation:

How to retrieve Membership's PasswordAnswer

It is encypted in the table. I don't want to reset the password. I got a solution at here

But I am not sure the namespace of

base.DecryptPassword

Because I got an error, can not find it.

Updated again:

updated my code:

public class FalseMembershipProvider: MembershipProvider
{
    public string GetPasswordAnswer(Guid providerUserKey)
    {
        Microsoft.Practices.EnterpriseLibrary.Data.Database db = Microsoft.Practices.EnterpriseLibrary.Data.DatabaseFactory.CreateDatabase();
        using (System.Data.Common.DbCommand cmd = db.GetSqlStringCommand("SELECT PasswordAnswer FROM aspnet_Membership WHERE UserID=@UserID"))
        {
            db.AddInParameter(cmd, "@UserId", DbType.Guid, providerUserKey);
            object answer = db.ExecuteScalar(cmd); if (answer != null)
                return ProviderDecryptor(answer.ToString());
            else
                return null;
        }
        db = null;
    }
    internal string ProviderDecryptor(string encryptedText)
    {
        string decrypted = null;
        if (!string.IsNullOrEmpty(encryptedText))
        {
            byte[] encodedbytes = Convert.FromBase64String(encryptedText);
            byte[] decryptedbytes = base.DecryptPassword(encodedbytes);
            if (decryptedbytes != null)
                decrypted = System.Text.Encoding.Unicode.GetString(decryptedbytes, 16, decryptedbytes.Length - 16);
        }
        return decrypted;
    }
}

Upvotes: 0

Views: 930

Answers (1)

keyboardP
keyboardP

Reputation: 69362

The class is inheriting from the MembershipProvider class. The method being called is MembershipProvider.DecryptPassword. However, as you can see on the MSDN page, it's a protected method. By inherting from MembershipProvider, this new class can use base.DecryptPassword which is essentially saying "call the DecryptPassword method of the MembershipProvider. Even though the method is protected, I can call it because I have permission since I'm inheriting from the MembershipProvider class".

The class you're writing needs to inherit from MembershipProvider as the author did in their example:

public class FalseMembershipProvider : MembershipProvider

Upvotes: 1

Related Questions