溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

ASP.NET加密口令的方法

發(fā)布時間:2021-07-22 22:39:44 來源:億速云 閱讀:383 作者:chen 欄目:開發(fā)技術(shù)

這篇文章主要介紹“ASP.NET加密口令的方法”,在日常操作中,相信很多人在ASP.NET加密口令的方法問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”ASP.NET加密口令的方法”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

每當我們要建立數(shù)據(jù)庫驅(qū)動的個人化的web站點時,都必須要保護用戶的數(shù)據(jù)。盡管黑客可以盜取個人的口令,然而更嚴重的問題是有人能夠盜走整個數(shù)據(jù)庫,然后立刻就是所有的口令。

原理

有一個好的做法是不將實際的口令存儲在數(shù)據(jù)庫中,而是存儲它們加密后的版本。當我們需要對用戶進行鑒定時,只是對用戶的口令再進行加密,然后將它與系統(tǒng)中的加密口令進行比較即可。

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

例子

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

下面是這樣的一個ASP.NET頁面源代碼:

ASPX文件:

復(fù)制代碼 代碼如下:


<%@ 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 >
<tr>
<td >登錄名</td>
<td><asp:TextBox id="userid" runat="server" Width="101px"></asp:TextBox></td>
</tr>
<tr>
<td >密碼</td>
<td><asp:TextBox id="pwd" runat="server" Width="101px"></asp:TextBox></td>
</tr>
<tr>
<td ><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文件:

復(fù)制代碼 代碼如下:


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ù)庫就ok了。加密口令就是這么簡單。

到此,關(guān)于“ASP.NET加密口令的方法”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI