我似乎不知道如何通过会员提供商使用 sha1 解密加密密码。
我不能在这里使用 .GetPassword() 方法,因为我正在从 sqldatasource 检索值并将它们放入 gridview 中。
这是网格视图:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="UserId" DataSourceID="SqlDataSource1"
EmptyDataText="There are no data records to display."
OnSelectedIndexChanged="GridView1_SelectedIndexChanged" >
<Columns>
<asp:CommandField ShowDeleteButton="True" ButtonType="Button" />
<asp:TemplateField HeaderText="Block users">
<ItemTemplate>
<asp:Button runat="server" ID="btnBlock" CommandName="Block" CommandArgument='<%# Eval("UserId") %>'
Text="Block" OnClick="btnBlock_Click" Visible='<%# !Convert.ToBoolean(Eval("IsLockedOut")) %>' />
<asp:Button runat="server" ID="btnDeblock" CommandName="Deblock" CommandArgument='<%# Eval("UserId") %>'
Text="Deblock" OnClick="btnBlock_Click" Visible='<%# Convert.ToBoolean(Eval("IsLockedOut")) %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Username">
<ItemTemplate>
<asp:Label ID="UserId" runat="server" Text='<%# Bind("UserId") %>' OnDataBinding="Decrypt" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="UserId" HeaderText="User id" ReadOnly="True"
SortExpression="UserId" />
<asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
<asp:BoundField DataField="LastLoginDate" HeaderText="Last login"
SortExpression="LastLoginDate" />
<asp:CheckBoxField DataField="IsLockedOut" HeaderText="Locked"
SortExpression="IsLockedOut" />
<asp:BoundField DataField="FailedPasswordAttemptCount"
HeaderText="Failed logins"
SortExpression="FailedPasswordAttemptCount" />
<asp:BoundField DataField="Comment" HeaderText="Comments"
SortExpression="Comment" />
</Columns>
</asp:GridView>
我已在 Templatefield 中用 itemtemplate 替换了 boundfield。itemtemplate 中的标签绑定到用户名,标签还有一个 OnDataBind="Decrypt" 应该解密标签的 Text 属性中的值。我一直在尝试一些我在网上找到的例子(甚至来自这个论坛),但我对 .net 的理解还没有那么好。这是我在解密()监听器中尝试的:
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;
}
我试图先解密用户名,我想这与密码的方法相同。为了消除进一步的问题,这是我在 web.config 中的设置
<membership defaultProvider="MembershipProvider">
<providers>
<clear/>
<add name="MembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="GustaafConnectionString"
applicationName="Gustaaf" enablePasswordRetrieval="true" enablePasswordReset="false" requiresQuestionAndAnswer="true"
requiresUniqueEmail="false" passwordFormat="Encrypted"/>
</providers>
</membership>
<machineKey validationKey="..." decryptionKey="..." validation="SHA1" decryption="AES"/>