首页 | 互联网 | IT动态 | IT培训 | Cisco | Windows | Linux | Java | .Net | Oracle | 软件测试 | C/C++ | 嵌入式开发 | 存储世界 | 服务器
网络设备 | IDC | 安全 | 求职招聘 | 数字网校 | 笔记本电脑 | 北大青鸟 | 技术专题 | 电子书下载 | 教学视频 | 源码下载 | 搜索 | 博客 | 论坛
中国IT实验室Dotnet频道
中国IT教育
Google
首页 ASP.NET  C#  XML/WebService ADO.NET VC.NET VB.NET .NET 资讯动态 专题 RSS订阅 讨论 下载
您现在的位置: 中国IT实验室 >> Dotnet >> .NET Framework >> 正文

.NET中带有口令加密的注册页面

在ASP.NET中提供了加密的功能。名字空间System.Web.Security中包含了类FormsAuthentication,其中有一个方法HashPasswordForStoringInConfigFile。这个方法可以将用户提供的字符变成乱码,然后存储起来。注意此方法是不能继承的。
下面的代码就是在做注册页面时将数据加密后存储到数据库的过程
Imports System.Web.Security
Imports System.Data
Imports System.Data.SqlClient  '////////所需要的名称空间

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
 Dim PassFormate As String
 '///////////////EncryptPassword调用函数
 PassFormate = EncryptPassword(uid.Text, "md5")   '//////////或者是EncryptPassword(uid.Text, "sha1")
 'TextBox2.Text = EncryptPassword(uid.Text, "md5")
 'TextBox3.Text = EncryptPassword(uid.Text, "sha1")
 '///////////这些大家自己试验吧
 'TextBox4.Text = FormsAuthentication.FormsCookieName
 'TextBox5.Text = FormsAuthentication.FormsCookiePath
 'TextBox6.Text = FormsAuthentication.GetRedirectUrl(uid.Text, True)
 'FormsAuthentication.SetAuthCookie(uid.Text, True)

 Dim sql As String = "insert into pwd(uid,pwd) values(@uid,@pwd)"
 Dim comm As SqlCommand = New SqlCommand(sql, conn)
 conn.Open()
 comm.Parameters.Add(New SqlParameter("@uid", SqlDbType.Char, 16))
 comm.Parameters("@uid").Value = uid.Text
 comm.Parameters.Add(New SqlParameter("@pwd", SqlDbType.Char, 16))
 comm.Parameters("@pwd").Value = PassFormate
 comm.ExecuteNonQuery()
 
   End Sub

   '////////////////定义加密函数,可以随时调用。
   Function EncryptPassword(ByVal password As String, ByVal passwordformate As String)
 If passwordformate = "sha1" Then
EncryptPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(password, "sha1")
 ElseIf passwordformate = "md5" Then
EncryptPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(password, "md5")
 Else
EncryptPassword = ""
 End If

   End Function
至于用户的验证也是一样的思路了。
【责编:Peng】

中国IT教育

相关产品和培训
文章评论
 友情推荐链接
 认证培训
 专题推荐

 ·算法分析与设计之五大常用算法
 ·开发必备 漫谈Java加密保护
 ·嵌入式开发--ARM技术专题
 ·C/C++指针,认真了解,灵活运用
 ·.NET开发:C#实用基础教程
 ·软件测试工具QTP学习专题
 ·嵌入式开发单片机解决方案专题
 ·Java开发环境 Greenfoot 程序员手册
 ·C++对象布局及多态实现的探索
 ·常见排序算法的实现
 今日更新
 社区讨论
 博客论点
 频道精选
 Dotnet频道相关导航