ASP.NET中MD5和SHA1密码保护算法的使用
作者:郁郁小蝎 来源:中国站长学院 发布时间:2007-08-24 09:18:00
标签:asp.net,md5,密码保护
你的主页或者你管理的网站有各种密码需要保护,把密码直接放在数据库或者文件中存在不少安全隐患,所以密码加密后存储是最常见的做法。在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。
0
投稿
猜你喜欢
- 1.INPUT和图片按钮对齐:<form method="post" action="
- 大家好,我叫斑马纹列表中使用两种相同的样式但颜色不同的背景,来间隔显示的内容。当然这个释义我是借鉴《designing interfaces
- 微软今天发布了SQL Server 2005 SP3的正式版,而这也将是该软件的最后一次升级服务,不过暂时只有英文版本,需要简体中文版的用户
- 关于target="_blank"去留的问题在网上已经被反复争议很多次了。有的说要留,有的说要去掉。主张留的一方主要是考
- 样式表是一种为超文本标签语言提供增强补充服务的技术,可对每一个html的标签做精雕细刻的修饰。只用html制作的网页,对页面内各部分的修饰能
- Oracle :NvlNVL函数:NVL函数是将NULL值的字段转换成默认字段输出。NVL(expr1,expr2)expr1,需要转换的字
- 1、HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Eventlog\Applicatio
- 为什么能实现在线编辑呢? 首先需要ie 的支持,在 ie 5.5以后就有一个编辑状态,就是利用这个编辑状态,然后用javascript 来控
- 官方网站:http://www.extjs.com/演示地址:http://www.yaohaixiao.com/effects/sampl
- 通过《SQL Server 数据库备份和还原认识和总结(一)》,相信您对数据备份和还原有了一个更深入的认识,在上文中我没有对事务日志做剖析,
- 网页采用了 UTF-8 编码格式,这本来没有问题,问题是外部 CSS 文件默认是 ANSI
- 在access中进行时间的比较sql语句很简单,如select * from table where thetime>#\"
- 在认识ImageMagick之前,我使用的图像浏览软件是KuickShow,截图软件是KSnapShot,这两款软件都是KDE附带的软件,用
- 由于一些读者对于960 Grid System CSS Framework的原理和使用方法比较感兴趣,暴风彬彬今天将和大家一同分享这篇关于9
- LOAD DATA INFILE '文件地址' INTO TABLE 表名 FIELDS TERMINATED BY
- 内容摘要:近年来,视频点播VOD(Video on Demand)的名字在媒体上出现得越来越多。VOD技术使人们可以根据
- 1.双击setup.exe.(出现安装向导界面) 2.在安装向导界面:选"基本安装",并选好主目录位置;创建启动数据库(
- 本篇文档旨在介绍如何安装配置基于2台服务器的MySQL集群。并且实现任意一台服务器出现问题或宕机时MySQL依然能够继续运行。注意!虽然这是
- 背景尽管到目前为止HTML4和XHTML1仍能够很好地满足我们的要求,但是它们仍然存在不足。为了满足用户丰富的基于Web应该程序的需要,达到
- 源码: 代码如下: <% '隐藏并修改文件的最后修改时间的aspshell '原理:通过FSO可以修改文件的属性,比