04/04/2013

How to make user id and password case sensitive ? VB.Net Code for beginners

After developing a software we try many things to make the software security proof and in that process we can try to make the user id and password case sensitive. for doing this these are the VB.Net code which I used in my program and it is working fine.

Imports System.Data.OleDb
Imports System.IO
Public Class FrmLogin
    Inherits System.Windows.Forms.Form
    Dim cn As New OleDbConnection
    Dim cmd As New OleDb.OleDbCommand
    Public LoginSucceeded As Boolean
    Dim Counter As Integer
    Dim bm As BindingManagerBase
    Private Sub FrmLogin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        cn.ConnectionString = Moduledatasource
        cn.Open()
        LoginSucceeded = False
    End Sub

    Private Sub BtnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnOk.Click
        Try
            If Counter = 8 Then
                MsgBox("You have exceeded the number of attempts")
                Frmsplash.Close()
                Me.Close()
                Exit Sub
            End If
            If TxtUserId.Text = "" Or TxtPassword.Text = "" Then
                MsgBox("You must enter a user name and password.", _
                vbOKOnly + vbInformation)
                TxtUserId.Focus()
            else
                FrmMainMDI.Show()
                Me.Hide()
                Me.log()

            Else

                Dim CmdER As New OleDbCommand
                Dim MyReader As OleDb.OleDbDataReader
                CmdER.Connection = cn
                CmdER.CommandText = "Select * from UserAccount where UserId = '" & TxtUserId.Text & " 'and UserPassword =  '" & TxtPassword.Text & "'"
                MyReader = CmdER.ExecuteReader

                If MyReader.Read() Then
                    'to make the User Id and Password case snsitive
                    If TxtPassword.Text <> MyReader("UserPassword").ToString Or TxtUserId.Text <> MyReader("UserId").ToString Then
                        MessageBox.Show(" User ID and Password is case sensitive. Login Denied ", " Error! ", MessageBoxButtons.OK, MessageBoxIcon.Error)
                        TxtPassword.Text = ""
                        TxtPassword.Focus()

                    Else
                      
                        TxtUserAccount.Text = MyReader("AccountType")
                        If TxtUserAccount.Text = "A" Then
                            FrmMainMDI.Show()
                            Me.Hide()
                            FrmMainMDI.DataBackupToolStripMenuItem.Enabled = True
                            Me.log()
                        Else
                            FrmMainMDI.Show()
                            Me.Hide()
                            FrmMainMDI.UtilityToolStripMenuItem.Enabled = False
                            FrmMainMDI.PrintToolStripMenuItem.Enabled = False
                            FrmMainMDI.ReportToolStripMenuItem.Enabled = False
                            Me.log()
                        End If
                       
                    End If
                Else

                    MsgBox("Your user ID or Password is wrong")
                    TxtPassword.Text = ""
                    TxtUserId.Text = ""
                    TxtUserId.Focus()
                End If
                LoginSucceeded = True
            End If

        Catch ex As Exception
            Dim MessageString As String = "Report this error to the system administrator: " & ControlChars.NewLine & ex.Message
            Dim TitleString As String = "Employee Master Details Data Load Failed"
            MessageBox.Show(MessageString, TitleString, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub 
The above code is to check the user id case sensitivity and also type of user like admin or normal. Please comment your view.

No comments:

Post a Comment

What & How