当前位置: 移动技术网 > IT编程>开发语言>.net > ASP.NET加密口令的方法实例

ASP.NET加密口令的方法实例

2017年12月12日  | 移动技术网IT编程  | 我要评论
每当我们要建立数据库驱动的个人化的web站点时,都必须要保护用户的数据。尽管黑客可以盗取个人的口令,然而更严重的问题是有人能够盗走整个数据库,然后立刻就是所有的口令。

每当我们要建立数据库驱动的个人化的web站点时,都必须要保护用户的数据。尽管黑客可以盗取个人的口令,然而更严重的问题是有人能够盗走整个数据库,然后立刻就是所有的口令。

原理

有一个好的做法是不将实际的口令存储在数据库中,而是存储它们加密后的版本。当我们需要对用户进行鉴定时,只是对用户的口令再进行加密,然后将它与系统中的加密口令进行比较即可。

在asp中,我们不得不借助外部对象来加密字符串。而.net sdk解决了这个问题,它在system.web.security名称空间中的formsauthentication类中提供了hashpasswordforstoringinconfigfile方法,这个方法的目的正如它的名字所提示的,就是要加密存储在form表单的口令。

例子

hashpasswordforstoringinconfigfile方法使用起来非常简单,它支持用于加密字符串的“sha1”和“md5”散列算法。为了看看“hashpasswordforstoringinconfigfile”方法的威力,让我们创建一个小小的asp.net页面,并且将字符串加密成sha1和md5格式。

下面是这样的一个asp.net页面源代码:

aspx文件:

复制代码 代码如下:

<%@ page language="c#" codebehind="loginform.aspx.cs" autoeventwireup="false" inherits="konson.log.loginform" %>
<!doctype html public "-//w3c//dtd html 4.0 transitional//en" >
<html>
<head>
<title>loginform</title>
<meta name="generator" content="microsoft visual studio 7.0">
<meta name="code_language" content="c#">
<meta name="vs_defaultclientscript" content="javascript">
<meta name="vs_targetschema" content="http://schemas.microsoft.com/intellisense/ie5">
</head>
<body ms_positioning="gridlayout">
<form id="loginform" method="post" runat="server">
<table style="width: 205px; height: 86px">
<tr>
<td style="width: 78px">登录名</td>
<td><asp:textbox id="userid" runat="server" width="101px"></asp:textbox></td>
</tr>
<tr>
<td style="width: 78px">密码</td>
<td><asp:textbox id="pwd" runat="server" width="101px"></asp:textbox></td>
</tr>
<tr>
<td style="width: 78px"><asp:button id="login" runat="server" text="登 录"></asp:button></td>
<td><asp:button id="cancel" runat="server" text="取 消"></asp:button></td>
</tr>
</table>
</form>
</body>
</html>

code behind文件:

复制代码 代码如下:

using system;
using system.collections;
using system.componentmodel;
using system.data;
using system.drawing;
using system.web;
using system.web.sessionstate;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.htmlcontrols;
using system.web.security;

namespace konson.log
{
public class loginform : system.web.ui.page
{
protected system.web.ui.webcontrols.textbox userid;
protected system.web.ui.webcontrols.button login;
protected system.web.ui.webcontrols.button cancel;
protected system.web.ui.webcontrols.textbox pwd;
string epwd;
private void page_load(object sender, system.eventargs e)
{}
#region web form designer generated code
override protected void oninit(eventargs e)
{
initializecomponent();
base.oninit(e);
}

private void initializecomponent()
{   
this.login.click += new system.eventhandler(this.login_click);
this.load += new system.eventhandler(this.page_load);
}
#endregion

private void login_click(object sender, system.eventargs e)
{
epwd=formsauthentication.hashpasswordforstoringinconfigfile(pwd.text, "sha1");
//epwd=formsauthentication.hashpasswordforstoringinconfigfile(pwd.text, "md5");
response.write(epwd);
}
}
}


上面的代码中,你只要把加密后的epwd串写时数据库就ok了。加密口令就是这么简单。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网