ASP.NET加密口令的方法實(shí)例
每當(dāng)我們要建立數(shù)據(jù)庫(kù)驅(qū)動(dòng)的個(gè)人化的web站點(diǎn)時(shí),都必須要保護(hù)用戶的數(shù)據(jù)。盡管黑客可以盜取個(gè)人的口令,然而更嚴(yán)重的問題是有人能夠盜走整個(gè)數(shù)據(jù)庫(kù),然后立刻就是所有的口令。
原理
有一個(gè)好的做法是不將實(shí)際的口令存儲(chǔ)在數(shù)據(jù)庫(kù)中,而是存儲(chǔ)它們加密后的版本。當(dāng)我們需要對(duì)用戶進(jìn)行鑒定時(shí),只是對(duì)用戶的口令再進(jìn)行加密,然后將它與系統(tǒng)中的加密口令進(jìn)行比較即可。
在ASP中,我們不得不借助外部對(duì)象來加密字符串。而.NET SDK解決了這個(gè)問題,它在System.Web.Security名稱空間中的FormsAuthentication類中提供了HashPasswordForStoringInConfigFile方法,這個(gè)方法的目的正如它的名字所提示的,就是要加密存儲(chǔ)在Form表單的口令。
例子
HashPasswordForStoringInConfigFile方法使用起來非常簡(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í)數(shù)據(jù)庫(kù)就ok了。加密口令就是這么簡(jiǎn)單。
相關(guān)文章
如何利用HttpClientFactory實(shí)現(xiàn)簡(jiǎn)單的熔斷降級(jí)
這篇文章主要給大家介紹了關(guān)于如何利用HttpClientFactory實(shí)現(xiàn)簡(jiǎn)單的熔斷降級(jí)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07
IE下document.referrer 拒絕訪問的解決方法
原理就是給IE瀏覽器的頁(yè)面偷偷加了個(gè)鏈接,然后自動(dòng)點(diǎn)這個(gè)鏈接,于是referrer就能保留了,感興趣的朋友可以參考下2013-09-09
.NET的動(dòng)態(tài)編譯與WS服務(wù)調(diào)用詳解
這篇文章介紹了.NET的動(dòng)態(tài)編譯與WS服務(wù)調(diào)用詳解,有需要的朋友可以參考一下,希望對(duì)你有所幫助2013-07-07
把a(bǔ)spx頁(yè)面?zhèn)窝b成靜態(tài)html格式的實(shí)現(xiàn)代碼
把a(bǔ)spx頁(yè)面?zhèn)窝b成靜態(tài)html格式的實(shí)現(xiàn)代碼,主要是利于搜索引擎的收錄。2011-10-10
使用JavaScript代碼實(shí)現(xiàn)各種數(shù)據(jù)控件的反選功能 不要只做拖控件的菜鳥
在我們做許多項(xiàng)目的時(shí)候,會(huì)用到反選這個(gè)功能,但是我一般使用C#代碼創(chuàng)建數(shù)組遍歷實(shí)現(xiàn)功能,今天我想換一種語(yǔ)言實(shí)現(xiàn)一下,于是我就用JavaScript研究了一下怎么實(shí)現(xiàn)這個(gè)功能2011-12-12

