我有一个网页,它在页面加载时检查加密的 cookie 以确定用户身份。但是,当我在开发盒上本地测试页面时,我无权访问该 cookie。
以前,我使用应用程序设置来告诉页面是否处于开发模式,并且在开发模式下它会加载固定的用户身份。然后我发现了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 版本上传到您的实时环境。
根据经验,信任客户端的任何内容都不是一个好主意。
我也会务实一点,你在保护什么,有人会付出多少努力来入侵?
下面的帖子探讨了您不应该信任它的一些原因:
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;
}
关于asp.net - Request.IsLocal 安全吗还是可能被欺骗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19010217/