Obtain UserName and Password from Online Text File - vb.net

I have the following code:
Dim userName As String
Dim passWord As String
'
' Populate userName and passWord from an online text file ...
'
If textbox1.text = userName AndAlso Textbox2.text = passWord Then
MsgBox("Welcome")
Else
MsgBox("UserName or Password incorrect")
End If
How do I verify the user and password against an online text file from a URL that contains data like:
User;Password

You can use WebClient class methods - DownloadFile or DownloadString or DownloadData to read the URL.
EDIT: Use String.Split() method to separate username and password.
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim wc As New WebClient
Dim strings As String
strings = wc.DownloadString("weebly.com/uploads/9/5/8/9/9589176/passwords.txt";)
wc.Dispose()
Dim ar() as String = strings.Split(New String() {" "}, StringSplitOptions.RemoveEmptyEntries)
IF ar(0)=TextBox1.Text and ar(1)=TextBox2.Text Then
MessageBox.Show("Welcome ", "Welcome", MessageBoxButtons.OK, MessageBoxIcon.Information)
Form2.Show()
Else
MsgBox("Wrong Password Try Again")
End If
End Sub

Related

I am looking to use textfiles to validate a username and password login

I am looking to use textfiles to validate a username and password in VB.NET.
I have the username validated but I can not validate the passsword and anything entered in txtpassowrd.text will result in a login.
The code I used is this:
Imports System.IO
Public Class frmReceptionist
Function IsInFile(ByVal person As String) As Boolean
If File.Exists("receptionistUser.txt") And File.Exists("receptionistPassword.txt") Then
Dim sr As StreamReader = File.OpenText("receptionistUser.txt")
Dim individual As String
Do Until sr.EndOfStream
individual = sr.ReadLine
If individual = person Then
sr.Close()
Return True
End If
Loop
sr.Close()
End If
Return False
End Function
Private Sub btnConfirm_Click(sender As Object, e As EventArgs) Handles btnConfirm.Click
'Determine if a person is in the file
Dim person As String = txtUsername.Text
If person <> "" Then
If IsInFile(person) Then
MessageBox.Show(person & " Welcome Receptionist", "Bia Duitse")
Me.Hide()
frmBiaDuitse.Show()
Else
MessageBox.Show(person & " Incorrect Login", "No")
End If
Else
MessageBox.Show("You must enter Details", "Information")
End If
txtUsername.Clear()
txtUsername.Focus()
End Sub
Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
Me.Hide()
frmSelectJob.Show()
End Sub
End Class
This is definitely not the way you should be doing this.
For learning purposes, you could load up your files into a Dictionary() like this:
Private Credentials As Dictionary(Of String, String)
Private Sub LoadCredentials()
If IsNothing(Credentials) Then
Credentials = New Dictionary(Of String, String)()
If File.Exists("receptionistUser.txt") And File.Exists("receptionistPassword.txt") Then
Dim users() As String = File.ReadAllLines("receptionistUser.txt")
Dim passwords() As String = File.ReadAllLines("receptionistPassword.txt")
If users.Length = passwords.Length Then
For i As Integer = 0 To users.Length - 1
Credentials.Add(users(i), passwords(i))
Next
End If
End If
End If
End Sub
Function IsInFile(ByVal person As String) As Boolean
LoadCredentials()
If Not IsNothing(Credentials) Then
Return Credentials.ContainsKey(person)
End If
Return False
End Function
Function Checkpassword(ByVal person As String, ByVal password As String) As Boolean
LoadCredentials()
If Not IsNothing(Credentials) Then
Return Credentials.ContainsKey(person) AndAlso password = Credentials(person)
End If
Return False
End Function

how to create log in password in vb encrypted

Hi i have been searching the internet for 3 weeks now on how to create a log in interface with encrypted password . I was a to encrypt the text box for password but I dont know how to implement it . I am using linq to sql dbml to connect to my data based on text only I was able to get it can create but I want it to be more secure and more professionally looking with encrypted one. By the way I used wizard for creating database not hard coded it that is the way I know how to do it. I am totally noob in programming any help will do. Thanks
Public Class User_Log_In_v7
Dim admin As New GeneralsDataContext
Private Sub User_Log_InBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Me.Validate()
Me.User_Log_InBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.User_Log_In_DataSet)
End Sub
Private Sub User_Log_In_v7_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'User_Log_In_DataSet.User_Log_In' table. You can move, or remove it, as needed.
'Me.User_Log_InTableAdapter.Fill(Me.User_Log_In_DataSet.User_Log_In)
End Sub
Private Sub btnLogIn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogIn.Click
Try
Dim check = From Storage In admin.User_Log_Ins _
Where UsernameTextBox.Text = Storage.Username And PasswordTextBox.Text = Storage.Password
If Not check.Count = 0 Then
Membership_Information.Show()
Me.Hide()
UsernameTextBox.Text = ""
PasswordTextBox.Text = ""
Else
MsgBox("Please check username or password and try again", MsgBoxStyle.Exclamation, "")
UsernameTextBox.Text = ""
PasswordTextBox.Text = ""
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
This is the code I can from another tutorial for encrypting
Imports System.Security.Cryptography
Imports System.Text
Public Class Form1
Dim DES As New TripleDESCryptoServiceProvider
Dim MD5 As New MD5CryptoServiceProvider
'hash function
Function MD5Hash(value As String) As Byte()
Return MD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(value))
End Function
'Encryption
Function Encrypt(input As String, Key As String) As String
DES.Key = MD5Hash(Key)
DES.Mode = CipherMode.ECB
Dim buffer As Byte() = ASCIIEncoding.ASCII.GetBytes(input)
Return Convert.ToBase64String(DES.CreateEncryptor().TransformFinalBlock(buffer, 0, buffer.Length))
End Function

Not fetching username and password from textfile

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim FILE_NAME As String = Cashierpath
System.IO.File.Exists(FILE_NAME) ' current
Dim objReader As StreamReader
Dim user As String = TextBox1.Text
Dim password As String = TextBox2.Text
Dim check As String
'Global Variable
'Dim DirPath7 As String = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Scrap Data\Cashier Info\Cashiers\")
For Each filename As String In IO.Directory.EnumerateFiles(DirPath7, "*.txt")
Dim fName As String = IO.Path.GetFileName(filename)
If user = fName & ".txt" Then
objReader = New StreamReader(fName)
check = objReader.ReadToEnd()
If password = check Then
MessageBox.Show("Welcome " & user & "!")
Close()
My.Forms.Home.Show()
Else
MessageBox.Show("Username or Password is incorrect")
End If
End If
Next
End Sub
When the user enters their "username" and "password" into the textbox's, and clicks on this button, i want this button to check if there is a textfile with the name of the username entered, and if theres a file with that username it must then read it and check if the password matches the string inside the file. if it doesnt match it, it must then display a messagebox saying that "Username or password is incorrect", but nothing happens when i click on this button. No error message appears either.
Can someone take a look at my code and tell me what im doing wrong?
What you have there is an awful way to handle user credentials!
Read this for more information: Salted Password Hashing - Doing it Right
Regardless, you're way over-coding it.
Elsewhere in your app (correct use of Combine):
' Global Variable
Friend Shared DirPath7 As String = IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyDocuments, "Scrap Data", "Cashier Info", "Cashiers")
Button Handler:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim User As String = TextBox1.Text.Trim
Dim Pass As String = TextBox2.Text.Trim
' Assemble the full expected file path, even if it might be malformed.
' The first case check protects against malformed path.
Dim FilePath As String = IO.Path.Combine(DirPath7, String.Format("{0}.txt", User))
Select Case True
Case User.Length = 0
MsgBox("Username is empty", vbExclamation, "Error")
Case Pass.Length = 0
MsgBox("Password is empty", vbExclamation, "Error")
Case Not IO.File.Exists(FilePath)
MsgBox("No file for User", vbExclamation, "Error")
Case Not IO.File.ReadAllText(FilePath) = Pass
MsgBox("Wrong Password", vbExclamation, "Error")
Case Else
MsgBox(String.Format("Welcome {0}!", User), vbOKOnly, "Success")
My.Forms.Home.Show()
End Select
End Sub
Dim objReader As StreamReader
Dim user As String = TextBox1.Text
Dim password As String = TextBox2.Text
Dim check As String
Dim fname = Path.Combine(DirPath7, String.Format("{0}.txt", user))
If File.Exists(fname) Then
Using objreader As New StreamReader(fname)
'objReader = StreamReader(fname)
check = objreader.ReadToEnd()
password = check
MessageBox.Show("Welcome " & user & "!")
Close()
My.Forms.Home.Show()
End Using
Else : MessageBox.Show("file not found, no user exists")
End If
removed the extra ".txt"
Added "Do Using" . . ."End Using"

Sendmail Attachment Issue in VB.NET

I have a sendmail program in vb.net. I have attachments sending just fine. The problem is when there is NO ATTACHMENT, the program errors out. How do i handle no attachment to bypass and send with no attachment? The below works fine.
Yea yea i know, my naming convention is horrible. I will change once i have the logic 100% complete.
Imports System.Net.Mail
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If TextBox1.Text = "" Then
MsgBox("Please describe the issue you're having Bender, I'm not a mindreader!")
Exit Sub
Else
lblpleasewait.Visible = True
Delay(2000)
Dim Recipients As New List(Of String)
Recipients.Add("emtnap6#yahoo.com")
Dim FromEmailAddress As String = Recipients(0)
Dim Subject As String = "IT Help!"
Dim Body As String = TextBox1.Text
Dim UserName As String = "fakeout"
Dim Password As String = "fakeout"
Dim Port As Integer = 587
Dim Server As String = "smtp.gmail.com"
Dim Attachments As New List(Of String)
MsgBox(SendEmail(Recipients, FromEmailAddress, Subject, Body, UserName, Password, Server, Port, Attachments))
TextBox2.Text = "Attach Screenshot:"
TextBox2.TextAlign = HorizontalAlignment.Right
lblpleasewait.Visible = False
TextBox1.Text = ""
TextBox1.Focus()
End If
End Sub
Sub delay(ByVal delay_ms As Integer)
Dim tspan As New TimeSpan
Dim tstart = Now
While tspan.TotalMilliseconds < delay_ms
tspan = Now - tstart
Application.DoEvents()
End While
End Sub
Function SendEmail(ByVal Recipients As List(Of String), _
ByVal FromAddress As String, _
ByVal Subject As String, _
ByVal Body As String, _
ByVal UserName As String, _
ByVal Password As String, _
Optional ByVal Server As String = "smtp.gmail.com", _
Optional ByVal Port As Integer = 587, _
Optional ByVal Attachments As List(Of String) = Nothing) As String
Dim Email As New MailMessage()
Try
Dim SMTPServer As New SmtpClient
''For Each Attachment As String In Attachments
'' Email.Attachments.Add(New Attachment(Attachment))
'Next
Dim mailattach As String = TextBox2.Text
Dim attachment As System.Net.Mail.Attachment
attachment = New System.Net.Mail.Attachment(mailattach)
Email.Attachments.Add(attachment)
Email.From = New MailAddress(FromAddress)
For Each Recipient As String In Recipients
Email.Bcc.Add(Recipient)
Next
Email.Subject = Subject
Email.Body = Body
SMTPServer.Host = Server
SMTPServer.Port = Port
SMTPServer.Credentials = New System.Net.NetworkCredential(UserName, Password)
SMTPServer.EnableSsl = True
SMTPServer.Send(Email)
Email.Dispose()
Return "Email to Derek was successfully sent."
Catch ex As SmtpException
Email.Dispose()
Return "Sending Email Failed. Smtp Error."
Catch ex As ArgumentOutOfRangeException
Email.Dispose()
Return "Sending Email Failed. Check Port Number."
Catch Ex As InvalidOperationException
Email.Dispose()
Return "Sending Email Failed. Check Port Number."
End Try
End Function
Private Sub FolderBrowserDialog1_HelpRequest(sender As Object, e As EventArgs)
If (FolderBrowserDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then
TextBox2.Text = FolderBrowserDialog1.SelectedPath
End If
End Sub
Private Sub Button2_Click(sender As Object, e As System.EventArgs) Handles Button2.Click
OpenFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
If (OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then
TextBox2.Text = OpenFileDialog1.FileName
End If
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.Focus()
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
If Not Environment.Is64BitProcess Then
Process.Start("C:\Windows\sysnative\SnippingTool.exe")
Else
Process.Start("C:\Windows\system32\SnippingTool.exe")
End If
End Sub
Private Sub OpenFileDialog1_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
OpenFileDialog1.InitialDirectory = Environment.SpecialFolder.MyComputer
End Sub
End Class
This should solve the problem.
If TextBox2.text <> "" then
Dim mailattach As String = TextBox2.Text
Dim attachment As System.Net.Mail.Attachment
attachment = New System.Net.Mail.Attachment(mailattach)
Email.Attachments.Add(attachment)
End if

about vb2010 connect .mdb file

i make the vb homework
first, connect the mdb file, then login.
In the mdb file, there have login account and password.
when i run the program, it have some problem:
"dataAdapter.Fill(dt)" highlighted, The 'Microsoft.Jet.OLEDB.4.0xxxxxxx.mdb' provider is not registered on the local machine.
Private Sub loginButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles loginButton.Click
Dim connStr As String = "Provider=Microsoft.Jet.OLEDB.4.0" & "Data Source=xxxxxxx.mdb"
Dim sqlStr As String = "Select * from account"
Dim dataAdapter As New OleDb.OleDbDataAdapter(sqlStr, connStr)
dataAdapter.Fill(dt)
dataAdapter.Dispose()
For i As Integer = 0 To (dt.Rows.Count - 1)
If TextBox1.Text = CStr(dt.Rows(i)("accountid")) And TextBox2.Text = CStr(dt.Rows(i)("password")) Then
login = True
End If
Next
If login = True Then
MsgBox("logined")
Else
MsgBox("Incorrect username or password. Please try again.")
TextBox1.Clear()
TextBox2.Clear()
End If
You're missing a semi-colon (;) in your connection string.