为什么我的if else返回false?

时间:2013-10-24 14:35:48

标签: asp.net vb.net

在我的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

1 个答案:

答案 0 :(得分:0)

首先,这甚至不应该编译。显然它确实如此,所以你必须拥有Option Strict OffRequest.UserHostAdress返回一个复杂对象,并将变量声明为字符串。我怀疑你真正想要的是那个物体的某些属性,虽然我不知道是哪一个。