我是VB .NET的新手,我想编写一种方法,无论有人尝试成功还是失败,每次有人尝试登录时都会将新条目保存到数据库中。 另外,我还需要捕获执行尝试的机器的IP地址。 有关从何处开始的任何建议
答案 0 :(得分:1)
我不认为可以在没有库的情况下在本地进行ip捕获,但是如果您不介意api调用,则可以使用ip-api.com
'Query location data
Dim Req As HttpWebRequest
Dim Ret As HttpWebResponse = Nothing
Dim SR As StreamReader
Req = DirectCast(WebRequest.Create("http://ip-api.com/json"), HttpWebRequest)
Ret = DirectCast(Req.GetResponse(), HttpWebResponse)
SR = New StreamReader(Ret.GetResponseStream())
Dim Raw As String = Nothing
Raw = SR.ReadToEnd()
Dim JavaScriptSerialization As New JavaScriptSerializer()
Dim ipdata_object As New IPData()
ipdata_object = JavaScriptSerialization.Deserialize(Raw, ipdata_object.GetType)
dim ip_address as string = ipdata_object.query.tostring()
IPData类
'IP Data API Deserialization classes for http://ip-api.com/json API response
<Serializable>
Class IPData
Public asinfo As String
Public city As String
Public country As String
Public countryCode As String
Public isp As String
Public lat As String
Public lon As String
Public org As String
Public query As String
Public region As String
Public regionName As String
Public status As String
Public timezone As String
Public zip As String
End Class
关于日志记录,取决于您要使用的是Mysql,CSV,Flatfile等
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'SAVE FUNCTION
Dim LogString as String = "User " + Username + " Attempted to login from " + ip_address + " Successfully/Failed"
My.Computer.FileSystem.WriteAllText("C://testfile.txt", inputString, True)
End Sub
答案 1 :(得分:0)
您可以为mysql数据库尝试类似的操作
Imports MySql.Data.MySqlClient
Imports System.Data.Sql
Imports System
Imports System.Data
Public Class LoginForm
'These variables store information regarding the sql connection string, and the sql class callers.
Dim connectionString As String = "Server=localhost; User Id=admin; Password=pass; Database=access_logs"
Dim SQLConnection As MySqlConnection = New MySqlConnection
Private Sub LoginButton_Click(sender As Object, e As EventArgs) Handles LoginButton.Click
Dim username As String = "usernametextbox.text"
Dim ip_address As String = ipfetchfunction()
Dim status As String = Nothing
If username.ToString() = "valid" And Password.ToString() = "valid" Then
status = "authorized"
Else
status = "failed"
End If
LogAttempt(username, ip_address, status)
End Sub
Public Function LogAttempt(user_name As String, ip_address As String, login_status As String) As Boolean
SQLConnection = New MySqlConnection()
SQLConnection.ConnectionString = connectionString
Dim sqlCommand As New MySqlCommand
Dim str_logSql As String
Try
str_logSql = "insert into access_log (username, user_ip, login_status) values ('" + user_name + "','" + ip_address + "','" + login_status + "')"
'MsgBox(str_logSql) uncomment for debugging
sqlCommand.Connection = SQLConnection
sqlCommand.CommandText = str_logSql
sqlCommand.ExecuteNonQuery()
Return True
Catch ex As Exception
Return False
'MessageBox.Show(ex.ToString()) Uncomment for debugging
End Try
End Function
End Class