實(shí)例分析ASP.NET加密口令

每當(dāng)我們要建立數(shù)據(jù)庫(kù)驅(qū)動(dòng)的個(gè)人化的web站點(diǎn)時(shí),都必須要保護(hù)用戶的數(shù)據(jù)。無(wú)論對(duì)于誰(shuí),安全問(wèn)題都是非常重要的。為了解決這個(gè)問(wèn)題,我給大家提供一個(gè)簡(jiǎn)單實(shí)用,但是老套的方法,就是口令加密。在此我們使用ASP.NET技術(shù)對(duì)口令加密。簡(jiǎn)單的講,就是將用戶提供的口令加密之后,然后讓它和存放于系統(tǒng)中的數(shù)據(jù)比較,如果相同,則通過(guò)驗(yàn)證。

ASP.NET加密口令原理:

當(dāng)我們需要對(duì)用戶進(jìn)行鑒定時(shí),只是對(duì)用戶的口令再進(jìn)行加密,然后將它與系統(tǒng)中的加密口令進(jìn)行比較即可。

在ASP中,我們不得不借助外部對(duì)象來(lái)加密字符串。而.NET SDK解決了這個(gè)問(wèn)題,它在System.Web.Security名稱空間中的FormsAuthentication類中提供了HashPasswordForStoringInConfigFile方法,這個(gè)方法的目的正如它的名字所提示的,就是要加密存儲(chǔ)在Form表單的口令。

ASP.NET加密口令實(shí)例:

HashPasswordForStoringInConfigFile方法使用起來(lái)非常簡(jiǎn)單,它支持用于加密字符串的“SHA1”和“MD5”散列算法。為了看看“HashPasswordForStoringInConfigFile”方法的威力,讓我們創(chuàng)建一個(gè)小小的ASP.NET頁(yè)面,并且將字符串加密成SHA1和MD5格式。

下面是這樣的一個(gè)ASP.NET頁(yè)面源代碼:

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串寫成數(shù)據(jù)庫(kù)就ok了。ASP.NET加密口令就這么簡(jiǎn)單,不知道您是否學(xué)會(huì)了。

小知識(shí)之ASP.NET:

ASP.NET 是一種統(tǒng)一的 Web 平臺(tái),它提供了生成企業(yè)級(jí)應(yīng)用程序所必需的全部服務(wù)。
ASP.NET 又是基于 .NET Framework 生成的,因此整個(gè)框架都可用于任何 ASP.NET 應(yīng)用程序。您可以使用任何與公共語(yǔ)言運(yùn)行庫(kù)兼容的語(yǔ)言(包括 Microsoft Visual Basic、Visual C# 和 JScript .NET)來(lái)創(chuàng)作應(yīng)用程序。以下章節(jié)將概述 ASP.NET 提供的功能。