RE:Abrir y cerrar formularios windows forms
Para un correcto uso de la aplicacion de logeo te pongo este ejemplo, te parecera extenso pero as ies como debe hacerse..........
1 Clase (Login) Encripta los datos que introduce y lo compara con el usuario
almacenado en uan BD mediante Hash.
2 LoginForm Formulario dond eintroduces tu usuario y password a validar.
3 Tu aplicacion que llama al LoginForm (esta aplicacion se abre y llama al loginform, si el
usuario es validado entonces se cierra loginform y
continuas la ejecucion de tu programa de lo
contrario se cierra todo. )
1 Clase Login --------------------------------------------------------------------
Imports System.Security.Cryptography
Public Class Login
Public Sub New()
End Sub
Public Function EncriptPassword(ByVal password As String) As String
If password = "" Then
Return Nothing
Exit Function
End If
Dim bytPassword(), bytEncrypted() As Byte
Dim strEncryptedPassword As String = ""
Dim sec As New SHA1CryptoServiceProvider
Dim ue As New System.Text.UTF8Encoding
Dim PassEncrypted As String = ""
Try
bytPassword = ue.GetBytes(password)
bytEncrypted = sec.ComputeHash(bytPassword)
PassEncrypted = Convert.ToBase64String(bytEncrypted)
Catch exc As Exception
MsgBox("Un error ha ocurrido en la encriptación.")
End Try
Return PassEncrypted
End Function
End Class
2 Login Form ---------------------------------------------------------------------
''estos imports pertenecen a mi capa de datos.....
Imports IntegralServices.BusinessLayer.BusinessObjects
Imports IntegralServices.Facade
Imports System.Text
Public Class LoginForm
Dim Login As New Login
Dim OperadorLogin As OperadoresDS ' este es un dataset (cambialo por el tuyo)
Dim intentos As Short = 0
Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
intentos += 1
If intentos <= 3 Then
Dim password As String = Login.EncriptPassword(Me.txtPassword.Text)
Dim User As String = Me.txtUserName.Text
'Aqui hago una consulta sobre el usuario y su password en un dataset
OperadorLogin = OperadoresFacade.GetOperadoresDS("Usuario='" & Me.txtUserName.Text & "' and Password='" & password & "'", "IdOperador ASC")
'Si coincide pasamos datos al form1 y continua el programa.
If OperadorLogin.Tables(0).Rows.Count > 0 Then
Form1.UserAuthenticated = True
Form1.UserName = Me.txtPassword.Text
'Form1.UserRole = OperadorLogin.Tables(0).Rows(0).Item("IdRol").ToString
Me.Close()
Else
Me.txtPassword.Text = ""
Me.txtUserName.Text = ""
MessageBox.Show("Usuario y Password incorrectos.")
Me.txtUserName.Focus()
End If
Else
MessageBox.Show("Demasiados intentos. Saliendo de la aplicación")
Me.Close()
Exit Sub
End If
End Sub
Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
Me.Close()
End Sub
Private Sub txtUserName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtUserName.TextChanged
If txtUserName.TextLength > 0 And txtPassword.TextLength > 0 Then
OK.Enabled = True
Else
OK.Enabled = False
End If
End Sub
Private Sub txtPassword_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtPassword.TextChanged
If txtUserName.TextLength > 0 And txtPassword.TextLength > 0 Then
OK.Enabled = True
Else
OK.Enabled = False
End If
End Sub
End Class
3 TuAplicacion ---------------------------------------------------------------------
Public Class Form1
Private _UserAuthenticated As Boolean = False
Private _UserName As String = ""
Private _UserRole As String = ""
Public Property UserName() As String
Get
Return _UserName
End Get
Set(ByVal value As String)
_UserName = value
End Set
End Property
Public Property UserRole() As String
Get
Return _UserRole
End Get
Set(ByVal value As String)
_UserRole = value
End Set
End Property
Public Property UserAuthenticated() As Boolean
Get
Return _UserAuthenticated
End Get
Set(ByVal value As Boolean)
_UserAuthenticated = value
End Set
End Property
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Presentamos el for mde validacion
My.Forms.LoginForm.ShowDialog()
If Me.UserAuthenticated Then
'Aqui el usuario esta autenticado
Else
'El usuario no esta autenticado, (despues de los 3 intentos en LoginForm
'nos devolvera UserAuuthenticated=false, cerramos la aplicacion)
Me.Close()
End If
End Sub
End Class