【发布时间】:2021-10-26 11:18:57
【问题描述】:
我想在一个MVC项目的web.config中设置IP,只有指定的IP可以访问该站点,但其他IP可以看到正在构建的视图。 谁能告诉我我该怎么做? 谢谢。
【问题讨论】:
标签: c# asp.net-mvc model-view-controller ip web-config
我想在一个MVC项目的web.config中设置IP,只有指定的IP可以访问该站点,但其他IP可以看到正在构建的视图。 谁能告诉我我该怎么做? 谢谢。
【问题讨论】:
标签: c# asp.net-mvc model-view-controller ip web-config
您必须使用 HTTP_X_FORWARDED_FOR 和 REMOTE_ADDR ServerVariables 获取客户端 IP 地址,然后匹配您的允许列表并根据您的条件执行进一步的操作。
string ip = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(ip))
{
ip = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
【讨论】: