在ASP
NET中提供了加密的功能
名字空間System
Web
Security中包含了類FormsAuthentication
其中有一個方法HashPasswordForStoringInConfigFile
這個方法可以將用戶提供的字符變成亂碼
然後存儲起來
注意此方法是不能繼承的
下面的代碼就是在做注冊頁面時將數據加密後存儲到數據庫的過程
Imports System
Web
Security
Imports System
Data
Imports System
Data
SqlClient
////////所需要的名稱空間
Private Sub Button
_Click(ByVal sender As System
Object
ByVal e As System
EventArgs) Handles Button
Click
Dim PassFormate As String
///////////////EncryptPassword調用函數
PassFormate = EncryptPassword(uid
Text
md
)
//////////或者是EncryptPassword(uid
Text
sha
)
TextBox
Text = EncryptPassword(uid
Text
md
)
TextBox
Text = EncryptPassword(uid
Text
sha
)
///////////這些大家自己試驗吧
TextBox
Text = FormsAuthentication
FormsCookieName
TextBox
Text = FormsAuthentication
FormsCookiePath
TextBox
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
))
comm
Parameters(
@uid
)
Value = uid
Text
comm
Parameters
Add(New SqlParameter(
@pwd
SqlDbType
Char
))
comm
Parameters(
@pwd
)
Value = PassFormate
comm
ExecuteNonQuery()
End Sub
////////////////定義加密函數
可以隨時調用
Function EncryptPassword(ByVal password As String
ByVal passwordformate As String)
If passwordformate =
sha
Then
EncryptPassword = FormsAuthentication
HashPasswordForStoringInConfigFile(password
sha
)
ElseIf passwordformate =
md
Then
EncryptPassword = FormsAuthentication
HashPasswordForStoringInConfigFile(password
md
)
Else
EncryptPassword =
End If
End Function
至於用戶的驗證也是一樣的思路了
From:http://tw.wingwit.com/Article/program/net/201311/11931.html