你的主页或者你管理的网站有各种密码需要保护,把密码直接放在数据库或者文件中存在不少安全隐患,所以密码加密后存储是最常见的做法。在ASP.NET中实现加密非常容易。.NET SDK中提供了CookieAuthentication类,其中的HashPasswordForStoringInConfigFile方法可直接使用MD5和SHA1算法。例子如下:
br>
file: encrypting.aspx
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
file:encrypting.cs
br>
br>
namespace encrypting
namespace encrypting
br>
{
{
br>
using System;
using System;
br>
using System.Collections;
using System.Collections;
br>
using System.ComponentModel;
using System.ComponentModel;
br>
using System.Data;
using System.Data;
br>
using System.Drawing;
using System.Drawing;
br>
using System.Web;
using System.Web;
br>
using System.Web.SessionState;
using System.Web.SessionState;
br>
using System.Web.UI;
using System.Web.UI;
br>
using System.Web.UI.WebControls;
using System.Web.UI.WebControls;
br>
using System.Web.UI.HtmlControls;
using System.Web.UI.HtmlControls;
br>
using System.Web.Security;
using System.Web.Security;
br>
///
///
summary>
br>
/// Summary description for encrypting.
/// Summary description for encrypting.
br>
///
///
/summary>
br>
public class encrypting : System.Web.UI.Page
public class encrypting : System.Web.UI.Page
br>
{
{
br>
protected System.Web.UI.WebControls.Label MD5;
protected System.Web.UI.WebControls.Label MD5;
br>
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Button Button1;
br>
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.TextBox TextBox1;
br>
br>
public encrypting()
public encrypting()
br>
{
{
br>
Page.Init += new System.EventHandler(Page_Init);
Page.Init += new System.EventHandler(Page_Init);
br>
}
}
br>
protected void Page_Load(object sender, EventArgs e)
protected void Page_Load(object sender, EventArgs e)
br>
{
{
br>
if (!IsPostBack)
if (!IsPostBack)
br>
{
{
br>
//
//
br>
// Evals true first time browser hits the page
// Evals true first time browser hits the page
br>
//
//
br>
}
}
br>
}
}
br>
protected void Page_Init(object sender, EventArgs e)
protected void Page_Init(object sender, EventArgs e)
br>
{
{
br>
//
//
br>
// CODEGEN: This call is required by the ASP+ Windows Form Designer.
// CODEGEN: This call is required by the ASP+ Windows Form Designer.
br>
//
//
br>
InitializeComponent();
InitializeComponent();
br>
}
}
br>
///
///
summary>
br>
/// Required method for Designer support - do not modify
/// Required method for Designer support - do not modify
br>
/// the contents of this method with the code editor.
/// the contents of this method with the code editor.
br>
///
///
/summary>
br>
private void InitializeComponent()
private void InitializeComponent()
br>
{
{
br>
Button1.Click += new System.EventHandler (this.Button1_Click);
Button1.Click += new System.EventHandler (this.Button1_Click);
br>
this.Load += new System.EventHandler (this.Page_Load);
this.Load += new System.EventHandler (this.Page_Load);
br>
}
}
br>
public void Button1_Click (object sender, System.EventArgs e)
public void Button1_Click (object sender, System.EventArgs e)
br>
{
{
br>
MD5.Text = CookieAuthentication.HashPasswordForStoringInConfigFile(TextBox1.Text,"MD5");
MD5.Text = CookieAuthentication.HashPasswordForStoringInConfigFile(TextBox1.Text,"MD5");
br>
//SHA1 use CookieAuthentication.HashPasswordForStoringInConfigFile(TextBox1.Text,"SHA1");
//SHA1 use CookieAuthentication.HashPasswordForStoringInConfigFile(TextBox1.Text,"SHA1");
br>
}
}
br>
}
}
br>
}
}
br>
注意:类CookieAuthentication的namespace是System.Web.Security。
注意:类CookieAuthentication的namespace是System.Web.Security。
