asp.net – Request.IsLocal是安全还是可以欺骗?

我有一个网页,在页面加载时检查加密的cookie以确定用户身份.但是,当我在我的开发盒上本地测试页面时,我无法访问该cookie.

以前我使用appsetting来告诉页面它是否处于开发模式,并且在dev模式下它将加载固定的用户身份.然后我发现了Request.IsLocal

我可以这样检查一下:

if(Request.IsLocal){
   FormsAuthentication.SetAuthCookie("testUser",false);
}else{
   FormsAuthentication.SetAuthCookie(/*EncryptedCookieValue*/,false);
}

这样安全吗?恶意用户有什么方法可以欺骗IsLocal吗?

解决方法

我认为你的实际问题是,你如何才有开发功能

你可以使用:Environment.UserInteractive
http://msdn.microsoft.com/en-us/library/system.environment.userinteractive.aspx

在IIS或Windows服务中运行时返回false,当它们是用户界面(即开发时的Visual Studio)时返回true.

我认为这比DEBUG预处理器变量更好,因为行为更加一致,您可能会意外地将dll的DEBUG版本上传到您的实时环境,除非您的构建/发布过程非常紧密.

根据经验,从客户端信任任何东西都不是一个好主意.
我也是务实的,你在保护什么,有人会去做多少努力?

以下SO帖子介绍了您不应该信任它的一些原因:
Can I fool HttpRequest.Current.Request.IsLocal?

参考
您可以在http://referencesource.microsoft.com查看来源

public bool IsLocal { 
   get {
      String remoteAddress = UserHostAddress; 

      // if unkNown,assume not local
      if (String.IsNullOrEmpty(remoteAddress))
         return false; 

      // check if localhost 
      if (remoteAddress == "127.0.0.1" || remoteAddress == "::1") 
         return true;

      // compare with local address
      if (remoteAddress == LocalAddress)
         return true;

      return false;
   }

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....