Reputation:
I am trying to check the stored IP address of users when logging in, and compare with what is stored in the database. The IP address is being stored in the Comment column of the default aspnetdb.
The email is sending every time the user connects even if the IP address is the same. I would only like the email to send when the IP address is different than what is stored.
Protected Sub LoginUser_LoggingIn(sender As Object, e As System.Web.UI.WebControls.LoginCancelEventArgs) Handles LoginUser.LoggingIn
Dim CurrentUser As MembershipUser = Membership.GetUser(LoginUser.UserName)
If (CurrentUser IsNot Nothing) Then
Dim IPAddress As String = HttpContext.Current.Request.UserHostAddress
Dim CurrentIP As String = CurrentUser.Comment
If (IPAddress IsNot CurrentIP) Then
Dim IP_Change As New Thread(
Sub()
Dim mailObject As New System.Net.Mail.MailMessage()
Dim emailCredentials As New System.Net.NetworkCredential("[email protected]", "password")
mailObject.Subject = "User " & CurrentUser.ToString & " is connecting from a new IP Address ( " & IPAddress & " )"
mailObject.Body = "User is connecting from a new IP Address"
mailObject.To.Add("[email protected]")
mailObject.From = New System.Net.Mail.MailAddress("[email protected]")
mailObject.IsBodyHtml = True
Dim SmtpMail As New System.Net.Mail.SmtpClient("my.domain.com")
SmtpMail.UseDefaultCredentials = False
SmtpMail.EnableSsl = False
SmtpMail.Credentials = emailCredentials
SmtpMail.Port = 587
SmtpMail.Send(mailObject)
End Sub
)
IP_Change.Start()
CurrentUser.Comment = IPAddress
Membership.UpdateUser(CurrentUser)
End If
End If
End Sub
Upvotes: 0
Views: 95
Reputation: 13142
Seems you're running into an inconsistency with the behavior of IsNot
due to reference comparisons.
Change your comparison to something like this: If (IPAddress <> CurrentIP) Then
and it should work.
Upvotes: 2