ASP.NET中MD5和SHA1密码保护算法的使用
作者:郁郁小蝎 来源:中国站长学院 时间:2007-08-24 09:18:00
你的主页或者你管理的网站有各种密码需要保护,把密码直接放在数据库或者文件中存在不少安全隐患,所以密码加密后存储是最常见的做法。在ASP.NET中实现加密非常容易。.NET SDK中提供了CookieAuthentication类,其中的HashPasswordForStoringInConfigFile方法可直接使用MD5和SHA1算法。例子如下:
文件: encrypting.aspx
<%@ Page language="c#" Codebehind="encrypting.cs" AutoEventWireup="false" Inherits="encrypting.encrypting" %>
<html><head>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#"></head>
<body>
<form method="post" runat="server">
<p> </p>
<p>
<asp:TextBox id=TextBox1 runat="server"></asp:TextBox>
<asp:Button id=Button1 runat="server" Text="encrypting"></asp:Button></p>
<p>Encrypting Password(MD5):
<asp:Label id=MD5 runat="server"></asp:Label></p>
</form>
</body></html>
文件:encrypting.cs
namespace encrypting
{
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;
/// <summary>
/// Summary description for encrypting.
/// </summary>
public class encrypting : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label MD5;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.TextBox TextBox1;
public encrypting()
{
Page.Init += new System.EventHandler(Page_Init);
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//
// Evals true first time browser hits the page
//
}
}
protected void Page_Init(object sender, EventArgs e)
{
//
// CODEGEN: This call is required by the ASP+ Windows Form Designer.
//
InitializeComponent();
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
Button1.Click += new System.EventHandler (this.Button1_Click);
this.Load += new System.EventHandler (this.Page_Load);
}
public void Button1_Click (object sender, System.EventArgs e)
{
MD5.Text = CookieAuthentication.HashPasswordForStoringInConfigFile(TextBox1.Text,"MD5");
//SHA1 use CookieAuthentication.HashPasswordForStoringInConfigFile(TextBox1.Text,"SHA1");
}
}
}
注意:类CookieAuthentication的namespace是System.Web.Security。
标签:asp.net,md5,密码保护
0
投稿
猜你喜欢
SQL Server服务器内存升级后的烦恼
2008-12-22 10:55:00
什么样的分页案例才是好的
2007-11-23 19:08:00
JS数组方法汇总
2009-08-03 14:06:00
JavaScript性能优化小技巧,创建文档碎片
2010-03-31 18:27:00
XML轻松学习手册(3)XML的术语
2008-09-05 17:17:00
JS不同加载方式下的window.onload
2009-05-21 18:09:00
用ASP动态生成JS表单验证代码
2007-09-30 20:38:00
CPQuery 解决拼接SQL的新方法
2012-11-30 20:01:46
如何使数据库的ID字段自动加1?
2010-06-03 10:47:00
Ajax的错误处理机制探讨
2007-09-07 09:53:00
好用的JS图片预加载类
2007-08-13 13:49:00
王孟友教你如何设计标志(LOGO)
2008-04-17 13:30:00
PJBlog3优化——301定向跳转解决重复内容的问题
2009-05-20 10:40:00
《JavaScript语言精粹》
2009-04-03 11:27:00
CSS布局之浮动(三)自适应
2008-08-19 12:49:00
PHP抽象工厂模式Abstract Factory Pattern优点与实现方式
2023-05-25 03:04:57
修改新云CMS底部版权信息字数限制
2008-07-31 18:00:00
在IE下用getAttribute时要小心
2008-08-21 12:54:00
两个2008北京奥运会倒计时js代码
2008-06-11 13:26:00
Instr函数与InstrRev函数的区别
2008-07-07 16:53:00