title: asp.net encrypted membership password & username retrieval
body: <p>I can't seem to find out how I should decrypt the encrypted password using sha1 via the membership provider.</p>
  
  <p>I can't use the .GetPassword() method here because I'm retrieving the values from a sqldatasource and placing them into a gridview.</p>
  
  <p>Here's the gridview:</p>
  
  <pre><code>    &lt;asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
          DataKeyNames="UserId" DataSourceID="SqlDataSource1" 
          EmptyDataText="There are no data records to display." 
          OnSelectedIndexChanged="GridView1_SelectedIndexChanged" &gt;
          &lt;Columns&gt;
              &lt;asp:CommandField ShowDeleteButton="True" ButtonType="Button" /&gt;
              &lt;asp:TemplateField HeaderText="Block users"&gt;
                  &lt;ItemTemplate&gt;
                      &lt;asp:Button runat="server" ID="btnBlock" CommandName="Block" CommandArgument='&lt;%# Eval("UserId") %&gt;'
                          Text="Block" OnClick="btnBlock_Click" Visible='&lt;%# !Convert.ToBoolean(Eval("IsLockedOut")) %&gt;' /&gt;
                      &lt;asp:Button runat="server" ID="btnDeblock" CommandName="Deblock" CommandArgument='&lt;%# Eval("UserId") %&gt;'
                          Text="Deblock" OnClick="btnBlock_Click" Visible='&lt;%# Convert.ToBoolean(Eval("IsLockedOut")) %&gt;' /&gt;
                  &lt;/ItemTemplate&gt;
              &lt;/asp:TemplateField&gt;
              &lt;asp:TemplateField HeaderText="Username"&gt;
                  &lt;ItemTemplate&gt;
                      &lt;asp:Label ID="UserId" runat="server" Text='&lt;%# Bind("UserId") %&gt;' OnDataBinding="Decrypt" /&gt;
                  &lt;/ItemTemplate&gt;
              &lt;/asp:TemplateField&gt;
              &lt;asp:BoundField DataField="UserId" HeaderText="User id" ReadOnly="True"
                  SortExpression="UserId" /&gt;
              &lt;asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" /&gt;
              &lt;asp:BoundField DataField="LastLoginDate" HeaderText="Last login" 
                  SortExpression="LastLoginDate" /&gt;
              &lt;asp:CheckBoxField DataField="IsLockedOut" HeaderText="Locked" 
                  SortExpression="IsLockedOut" /&gt;
              &lt;asp:BoundField DataField="FailedPasswordAttemptCount" 
                  HeaderText="Failed logins" 
                  SortExpression="FailedPasswordAttemptCount" /&gt;
              &lt;asp:BoundField DataField="Comment" HeaderText="Comments" 
                  SortExpression="Comment" /&gt;
          &lt;/Columns&gt;
      &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>I have replaced the boundfield with an itemtemplate in a Templatefield.
  The label inside the itemtemplate is bound to the username, the label has also an OnDataBind="Decrypt" that should decrypt the value in the Text attribute of the label.
  I've been trying a few examples I found online (even from this forum), but my understanding of .net is not that fantastic yet.
  Here's what I tried in the decrypt() listener:</p>
  
  <pre><code>public void Decrypt(object sender, EventArgs e)
  {
      Label lbl = (Label)sender;
      string decrypted = string.Empty;
      UTF8Encoding encode = new UTF8Encoding();
      Decoder Decode = encode.GetDecoder();
      byte[] todecode_byte = Convert.FromBase64String(lbl.Text);
      int charCount = Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
      char[] decoded_char = new char[charCount];
      Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
      decrypted = new String(decoded_char);
      lbl.Text = decrypted;
  }
  </code></pre>
  
  <p>I was trying to get the username decrypted first, I suppose it's the same method for the password.
  To eleminate further questions, here's my setup in web.config</p>
  
  <pre><code>&lt;membership defaultProvider="MembershipProvider"&gt;
    &lt;providers&gt;
      &lt;clear/&gt;
      &lt;add name="MembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="GustaafConnectionString" 
           applicationName="Gustaaf" enablePasswordRetrieval="true" enablePasswordReset="false" requiresQuestionAndAnswer="true" 
           requiresUniqueEmail="false" passwordFormat="Encrypted"/&gt;
    &lt;/providers&gt;
  &lt;/membership&gt;
  &lt;machineKey validationKey="..." decryptionKey="..." validation="SHA1" decryption="AES"/&gt;
  </code></pre>