简体   繁体   中英

Wrong usage of UserHostAddress

I have some web site with two .aspx files (Default.aspx and Default2.aspx). In Default.aspx.cs I have this simple code:

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string ip = Request.UserHostAddress.ToString();
        bool IsCorrectIP = ip.StartsWith("194.100.");
        if (IsCorrectIP)
        {
            Server.Transfer("Default2.aspx");
        }
    }
}

When I run my Default.aspx (locally) on IP address which doesn´t start with 194.100.(xxx), Default.aspx is still displayed. Could someone help me how to allow only certain IP in this sample ?

Your condition should look like this, add a NOT( ! ) before IsCorrectIP :

if (!IsCorrectIP)
{
    Server.Transfer("Default2.aspx");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM