Trước tiên bạn thiết kế form đăng nhập cơ bản như sau:
Tại ô mật khẩu các bạn điều chỉnh thuộc tính của Textbox chỉ hiển thị dạng dấu (*).
Hàm kiểm tra mật khẩu:
Public Function _kiemtra(ByVal strLenh As String) As Integer Dim intkt As Integer open_connect() Dim cmd As New SqlCommand(strLenh, strconnect) intkt = cmd.ExecuteScalar strconnect.Close() Return intkt End Function
Hàm mã hóa MD5
Public Function encryptData(ByVal data As String) As Byte() Dim md5Hasher As New System.Security.Cryptography.MD5CryptoServiceProvider() Dim hashedBytes As Byte() Dim encoder As New System.Text.UTF8Encoding() hashedBytes = md5Hasher.ComputeHash(encoder.GetBytes(data)) Return hashedBytes End Function Public Function md5(ByVal data As String) As String Return BitConverter.ToString(encryptData(data)).Replace("-", "").ToLower() End Function
Vấn đề bây giờ sử dụng như thể nào? Tại nút đăng nhập bạn viết như sau:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim intKiemTra As Integer = _kiemtra("select count(*) from tbl_user where tendangnhap= & txtTenDangNhap.Text & and matkhau= & md5(txtMatKhau.Text) & ") If intKiemTra = 1 Then MessageBox.Show("Đăng nhập thành công!") Else MessageBox.Show("Đăng nhập không thành công!") End If End Sub
Bây giờ bạn nhấn F5 để kiểm tra thành quả của mình xem nào?
Một lưu ý nhỏ,khi bạn tạo tài khoản người dùng để đăng nhập bằng cách lưu lại mật khẩu mã hóa MD5. Chúc các bạn thành công!
No comments:
Post a Comment