Option Explicit On
Option Strict On
Imports System.Data.SqlClient
Imports System.Data
Imports System.Globalization
Imports System.Text
Imports System.Drawing.Drawing2D
Imports System.IO
Imports System.Security.Cryptography
Public Class frmlogin
Dim Conn As SqlConnection
Dim com As SqlCommand
Dim dr As SqlDataReader
Dim tr As SqlTransaction
Dim sb As StringBuilder
Dim ms As MemoryStream
Dim desCrypt As DESCryptoServiceProvider
Dim cs As CryptoStream
Dim PwdWithEncrypt As String
Dim CurrentAuthentication As String
Dim IsChange As Boolean = False
Private Sub btncancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncancel.Click
Dim result As MsgBoxResult
result = CType(MessageBox.Show("คุณต้องการออกจากระบบหรือไม่", "ออกจากระบบ", MessageBoxButtons.YesNo, MessageBoxIcon.Question), MsgBoxResult)
If result = MsgBoxResult.Yes Then
End
End If
End Sub
Private Sub frmlogin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim strConn As String
strConn = DB.strConn
Conn = New SqlConnection()
With Conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strConn
.Open()
End With
End Sub
Private Sub btnok_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnok.Click
If (txtusername.Text.Trim() = "") OrElse (txtpassword.Text.Trim() = "") Then
txtusername.Focus()
Exit Sub
End If
Dim CurrentLogin As Boolean = False
CurrentLogin = Login(txtusername.Text, txtpassword.Text)
If CurrentLogin = True Then
DB.UserName = txtusername.Text.Trim()
DB.UserAuthentication = CurrentAuthentication
main_manu.Show()
Me.Hide()
Else
MessageBox.Show("UserName หรือ Password ที่คุณป้อน ไม่ถูกต้อง !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
CurrentAuthentication = ""
txtusername.Text = ""
txtusername.Focus()
txtpassword.Text = ""
End If
End Sub
Private Function Login(ByVal _UserName As String, ByVal _Password As String) As Boolean
sb = New StringBuilder()
sb.Append("SELECT UserName,Password,Auth")
sb.Append(" FROM UserName")
sb.Append(" WHERE (UserName=@UserName)")
sb.Append(" AND (Password=@Password)")
sb.Append(" AND (IsNormal='1')")
Dim sqlLogin As String
sqlLogin = sb.ToString()
Dim CurrentIV As Byte() = New Byte() {51, 52, 53, 54, 55, 56, 57, 58}
Dim CurrentKey As Byte() = {}
If _UserName.Length = 8 Then
CurrentKey = Encoding.ASCII.GetBytes(_UserName)
ElseIf _UserName.Length > 8 Then
CurrentKey = Encoding.ASCII.GetBytes(_UserName.Substring(0, 8))
Else
Dim i As Integer
Dim AddString As String = _UserName.Substring(0, 1)
Dim TotalLoop As Integer = 8 - CInt(_UserName.Length)
Dim tmpKey As String = _UserName
For i = 1 To TotalLoop
tmpKey = tmpKey & AddString
Next
CurrentKey = Encoding.ASCII.GetBytes(tmpKey)
End If
desCrypt = New DESCryptoServiceProvider
With desCrypt
.IV = CurrentIV
.Key = CurrentKey
End With
ms = New MemoryStream
ms.Position = 0
cs = New CryptoStream(ms, desCrypt.CreateEncryptor, CryptoStreamMode.Write)
Dim arrByte As Byte() = Encoding.ASCII.GetBytes(_Password)
cs.Write(arrByte, 0, arrByte.Length)
cs.FlushFinalBlock()
cs.Close()
PwdWithEncrypt = Convert.ToBase64String(ms.ToArray())
com = New SqlCommand()
With com
.CommandText = sqlLogin
.CommandType = CommandType.Text
.Connection = Conn
.Parameters.Clear()
.Parameters.Add("@UserName", SqlDbType.NVarChar).Value = _UserName
.Parameters.Add("@Password", SqlDbType.NVarChar).Value = PwdWithEncrypt
dr = .ExecuteReader()
If dr.HasRows Then
With dr
.Read()
CurrentAuthentication = .GetString(.GetOrdinal("Auth"))
End With
dr.Close()
Return True
Else
dr.Close()
Return False
End If
End With
End Function
End Class
Tag : .NET, MySQL, Ms SQL Server 2005, Ms SQL Server 2008