你主页或者你管理网站WebSite有各种密码需要保护把密码直接放在数据库或者文件中存在不少安全隐患所以密码加密后存储是最常见做法在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>
encrypting<br>
{<br>
using ;<br>
using .Collections;<br>
using .ComponentModel;<br>
using .Data;<br>
using .Drawing;<br>
using .Web;<br>
using .Web.SessionState;<br>
using .Web.UI;<br>
using .Web.UI.WebControls;<br>
using .Web.UI.HtmlControls;<br>
using .Web.Security;<br>
/// <summary><br>
/// Summary description for encrypting.<br>
/// </summary><br>
public encrypting : .Web.UI.Page<br>
{<br>
protected .Web.UI.WebControls.Label MD5;<br>
protected .Web.UI.WebControls.Button Button1;<br>
protected .Web.UI.WebControls.TextBox TextBox1;<br>
<br>
public encrypting<br>
{<br>
Page.Init .EventHandler(Page_Init);<br>
}<br>
protected void Page_Load(object sender, EventArgs e)<br>
{<br>
(!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 mody<br>
/// the contents of this method with the code editor.<br>
/// </summary><br>
private void InitializeComponent<br>
{<br>
Button1.Click .EventHandler (this.Button1_Click);<br>
this.Load .EventHandler (this.Page_Load);<br>
}<br>
public void Button1_Click (object sender, .EventArgs e)<br>
{<br>
MD5.Text = CookieAuthentication.HashPasswordForStoringInConfigFile(TextBox1.Text,"MD5");<br>
//SHA1 use CookieAuthentication.HashPasswordForStoringInConfigFile(TextBox1.Text,"SHA1");<br>
}<br>
}<br>
}<br>
注意:类CookieAuthentication是.Web.Security
最新评论