ASP.NET中密码保护,MD5和SHA1算法的使用

王朝asp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

你的主页或者你管理的网站有各种密码需要保护,把密码直接放在数据库或者文件中存在不少安全隐患,所以密码加密后存储是最常见的做法。在ASP.NET中实现加密非常容易。.NET SDK中提供了CookieAuthentication类,其中的HashPasswordForStoringInConfigFile方法可直接使用MD5和SHA1算法。例子如下:<br>

file: encrypting.aspx<br>

<%@ Page language="c#" Codebehind="encrypting.cs" AutoEventWireup="false" Inherits="encrypting.encrypting" %><br>

<html><head><br>

<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0"><br>

<meta name="CODE_LANGUAGE" Content="C#"></head><br>

<body><br>

<br>

<form method="post" runat="server"><br>

<p> </p><br>

<p><br>

<asp:TextBox id=TextBox1 runat="server"></asp:TextBox><br>

<asp:Button id=Button1 runat="server" Text="encrypting"></asp:Button></p><br>

<p>Encrypting Password(MD5):<br>

<asp:Label id=MD5 runat="server"></asp:Label></p><br>

</form><br>

<br>

</body></html><br>

<br>

file:encrypting.cs<br>

<br>

namespace encrypting<br>

{<br>

using System;<br>

using System.Collections;<br>

using System.ComponentModel;<br>

using System.Data;<br>

using System.Drawing;<br>

using System.Web;<br>

using System.Web.SessionState;<br>

using System.Web.UI;<br>

using System.Web.UI.WebControls;<br>

using System.Web.UI.HtmlControls;<br>

using System.Web.Security;<br>

/// <summary><br>

/// Summary description for encrypting.<br>

/// </summary><br>

public class encrypting : System.Web.UI.Page<br>

{<br>

protected System.Web.UI.WebControls.Label MD5;<br>

protected System.Web.UI.WebControls.Button Button1;<br>

protected System.Web.UI.WebControls.TextBox TextBox1;<br>

<br>

public encrypting()<br>

{<br>

Page.Init += new System.EventHandler(Page_Init);<br>

}<br>

protected void Page_Load(object sender, EventArgs e)<br>

{<br>

if (!IsPostBack)<br>

{<br>

//<br>

// Evals true first time browser hits the page<br>

//<br>

}<br>

}<br>

protected void Page_Init(object sender, EventArgs e)<br>

{<br>

//<br>

// CODEGEN: This call is required by the ASP+ Windows Form Designer.<br>

//<br>

InitializeComponent();<br>

}<br>

/// <summary><br>

/// Required method for Designer support - do not modify<br>

/// the contents of this method with the code editor.<br>

/// </summary><br>

private void InitializeComponent()<br>

{<br>

Button1.Click += new System.EventHandler (this.Button1_Click);<br>

this.Load += new System.EventHandler (this.Page_Load);<br>

}<br>

public void Button1_Click (object sender, System.EventArgs e)<br>

{<br>

MD5.Text = CookieAuthentication.HashPasswordForStoringInConfigFile(TextBox1.Text,"MD5");<br>

//SHA1 use CookieAuthentication.HashPasswordForStoringInConfigFile(TextBox1.Text,"SHA1");<br>

}<br>

}<br>

}<br>

注意:类CookieAuthentication的namespace是System.Web.Security。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航