ASP.NET加密口令的方法实例
每当我们要建立数据库驱动的个人化的web站点时,都必须要保护用户的数据。尽管黑客可以盗取个人的口令,然而更严重的问题是有人能够盗走整个数据库,然后立刻就是所有的口令。
原理
有一个好的做法是不将实际的口令存储在数据库中,而是存储它们加密后的版本。当我们需要对用户进行鉴定时,只是对用户的口令再进行加密,然后将它与系统中的加密口令进行比较即可。
在ASP中,我们不得不借助外部对象来加密字符串。而.NETSDK解决了这个问题,它在System.Web.Security名称空间中的FormsAuthentication类中提供了HashPasswordForStoringInConfigFile方法,这个方法的目的正如它的名字所提示的,就是要加密存储在Form表单的口令。
例子
HashPasswordForStoringInConfigFile方法使用起来非常简单,它支持用于加密字符串的“SHA1”和“MD5”散列算法。为了看看“HashPasswordForStoringInConfigFile”方法的威力,让我们创建一个小小的ASP.NET页面,并且将字符串加密成SHA1和MD5格式。
下面是这样的一个ASP.NET页面源代码:
ASPX文件:
<%@Pagelanguage="c#"Codebehind="loginform.aspx.cs"AutoEventWireup="false"Inherits="konson.log.loginform"%>
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.0Transitional//EN">
<HTML>
<HEAD>
<title>loginform</title>
<metaname="GENERATOR"Content="MicrosoftVisualStudio7.0">
<metaname="CODE_LANGUAGE"Content="C#">
<metaname="vs_defaultClientScript"content="JavaScript">
<metaname="vs_targetSchema"content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<bodyMS_POSITIONING="GridLayout">
<formid="loginform"method="post"runat="server">
<tablestyle="WIDTH:205px;HEIGHT:86px">
<tr>
<tdstyle="WIDTH:78px">登录名</td>
<td><asp:TextBoxid="userid"runat="server"Width="101px"></asp:TextBox></td>
</tr>
<tr>
<tdstyle="WIDTH:78px">密码</td>
<td><asp:TextBoxid="pwd"runat="server"Width="101px"></asp:TextBox></td>
</tr>
<tr>
<tdstyle="WIDTH:78px"><asp:Buttonid="login"runat="server"Text="登录"></asp:Button></td>
<td><asp:ButtonID="cancel"Runat="server"Text="取消"></asp:Button></td>
</tr>
</table>
</form>
</body>
</HTML>
CodeBehind文件:
usingSystem;
usingSystem.Collections;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Web;
usingSystem.Web.SessionState;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.HtmlControls;
usingSystem.Web.Security;
namespacekonson.log
{
publicclassloginform:System.Web.UI.Page
{
protectedSystem.Web.UI.WebControls.TextBoxuserid;
protectedSystem.Web.UI.WebControls.Buttonlogin;
protectedSystem.Web.UI.WebControls.Buttoncancel;
protectedSystem.Web.UI.WebControls.TextBoxpwd;
stringepwd;
privatevoidPage_Load(objectsender,System.EventArgse)
{}
#regionWebFormDesignergeneratedcode
overrideprotectedvoidOnInit(EventArgse)
{
InitializeComponent();
base.OnInit(e);
}
privatevoidInitializeComponent()
{
this.login.Click+=newSystem.EventHandler(this.login_Click);
this.Load+=newSystem.EventHandler(this.Page_Load);
}
#endregion
privatevoidlogin_Click(objectsender,System.EventArgse)
{
epwd=FormsAuthentication.HashPasswordForStoringInConfigFile(pwd.Text,"SHA1");
//epwd=FormsAuthentication.HashPasswordForStoringInConfigFile(pwd.Text,"MD5");
Response.Write(epwd);
}
}
}
上面的代码中,你只要把加密后的epwd串写时数据库就ok了。加密口令就是这么简单。
相关文章
- ASP.NET Core 6框架揭秘实例演示[31]:路由"高阶"用法
- 数据库使用.NET连接MySQL数据库(net连接mysql)
- .net让Oracle与ASP.NET实现无缝连接(oracleasp)
- .net安装 Linux 上的 ASP.NET 环境(linux安装asp)
- .Net搭配Redis解决高性能应用问题(.net redis)
- 用ASP搭建MySQL网站技术实例(asp mysql 网站)
- NET环境下 MySQL数据库应用实践(.net配合 mysql)
- asp.net下配置数据源时出现:未将对象引用设置到对象的实例。
- asp.net下数据库操作优化一例
- Asp.net在三层架构中事务的使用实例代码
- asp.net利用Ajax和Jquery在前台向后台传参数并返回值的实例
- ASP.NET连接SQL数据库的简单实例代码
- ASP.NET中用js取CheckBoxList中值的方法实例
- asp.net通用的连接数据库实例代码
- asp.net读取磁盘文件、删除实例代码
- ASP.NET实现TreeView的XML数据源绑定实例代码
- .Net读取Excel返回DataTable实例代码
- asp.net中利用Jquery+Ajax+Json实现无刷新分页的实例代码
- asp.net各种cookie代码和解析实例
- Winform实现调用asp.net数据接口实例
- ASP.Net巧用窗体母版页实例
- .NET中的async和await关键字使用及Task异步调用实例
- ASP.NET实现二维码(QRCode)的创建和读取实例
- asp.net使用Repeater控件中的全选进行批量操作实例
- asp.net+ajax的Post请求实例