asp.net 加密会员密码和用户名检索



我似乎找不到应该如何通过成员资格提供程序使用sha1解密加密的密码。

我不能用。GetPassword()方法,因为我从sqldatasource中检索值,并将它们放入网格视图中。

这是网格视图:

<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替换了边界字段。itemtemplate中的标签绑定到用户名,该标签还有一个OnDataBind="Decrypt",它应该解密标签的Text属性中的值。我一直在尝试一些我在网上找到的例子(甚至是在这个论坛上),但我对.net的理解还不是很好。以下是我在decrypt()监听器中尝试的内容:

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"/>

SHA1是一种哈希算法,而不是加密算法,根据定义,哈希是单向的,因此无法撤消。因此,您永远不会使用sha来"解密"任何内容,更不用说散列了。

您的数据似乎是通过AES加密的,而不是sha。此外,您还混淆了编码和加密。以下是有关在.net中使用AES的一些信息:http://msdn.microsoft.com/en-us/library/system.security.cryptography.aes.aspx

编码就是将字节数据解释为一种或另一种方案(ascii、unicode、UCT-8)中的字符,因此一旦解密了数据,您可能必须将其编码为字符串以供显示,但这是解密的次要步骤。

附录:您可能无法避免使用成员资格。GetPassword(),因为在成员身份数据库实现中使用的加密密钥可能无法检索。您是否考虑过使用对象数据源?然后可以使用在代码中预先填充一个条目列表。GetPassword()并将它们绑定到网格。

最新更新