如上所示,在我的 Appcode 文件夹中,我有一个名为 BaseClass 的类。在 BaseClass 中,我有一个名为 CheckWAN() 的函数,它定义了 IP 范围,以便稍后我可以通过本地用户的 IP 地址范围自动对我的站点进行身份验证。
Public Function CheckWAN() As Boolean
Try
Dim url As String = Request.Url.ToString()
'Get the client ip address
Dim RemoteAddress As String = Request.UserHostAddress
Dim strRemoteAddress As String = RemoteAddress
Dim myWAN As String = "192.168.254.254"
'add Some other ips
Dim SOther001 As String = "192.168.254.1"
Dim SOther002 As String = "192.168.254.2"
Dim SOther003 As String = "192.168.254.3"
If strRemoteAddress.Contains(myWAN) Then
Return True
ElseIf strRemoteAddress.Contains(SOther001) Then
Return True
ElseIf strRemoteAddress.Contains(SOther002) Then
Return True
ElseIf strRemoteAddress.Contains(SOther003) Then
Return True
Else
Return False
End If
Catch
Return False
End Try
End Function
最后,我在站点 default.aspx 上设置了一个登录名,如果If CheckWAN()
返回 true,则检查连接用户的 IP 地址,然后我会传递到内容页面,但是如果它是 false,那么它会向我显示带有消息的登录名它返回错误
Public Class BaseClass
Inherits System.Web.UI.Page
If CheckWAN() = True Then
Response.Redirect("/content.aspx")
Else
Response.Write("The CheckWAN is returning False")
'this else also causes a redirect loop if its changed to
'Response.Write(/default.aspx) not sure why
End If
我还检查了网络以验证我的代码中使用的 IP,它们都是有效的。
已编辑! 这是 Request.UserHostAdress 返回 debug