ASP 总结

回顾核心概念、常见陷阱清单、以及向现代技术迁移的路径。

Beginner
Key PointsASP 的核心是五个内置对象加 VBScript;安全上的关键是「一切输入都不可信」。

你学到了什么

  • 语法 —— Dim、If、For、Sub、Function,以及 Option Explicit
  • 五个对象 —— Request 读、Response 写、Session 存用户、Application 存全站、Server 造对象
  • 文件 —— FileSystemObject + TextStream,注意 UTF-8 要用 ADODB.Stream
  • 数据库 —— Connection / Recordset / Command,参数化防注入
  • 组件 —— AdRotator、BrowserCap、Content Linking、ContentRotator

陷阱清单

TrapConsequence
拼 SQL注入,可直接拖库
直接输出参数XSS
Application 不加锁计数丢失、数据错乱
文件带 BOMRedirect / Cookies 静默失效
输出后才 Redirect「HTTP 头已发送」
Request("x") 不指定集合参数被 URL 覆盖
上传目录可执行脚本上传 .asp 即获得执行权限
错误详情发到浏览器泄露路径与 SQL

安全检查

ASP
<%
    ' 1. 所有输出转义
    Response.Write Server.HTMLEncode(userInput)

    ' 2. 所有 SQL 参数化
    cmd.Parameters.Append cmd.CreateParameter("p", 200, 1, 100, userInput)

    ' 3. 所有文件路径校验
    If InStr(userInput, "..") = 0 And userInput Like "[A-Za-z0-9_.-]*" Then
        ' 安全
    End If

    ' 4. 所有重定向目标白名单
    If Left(url, 22) = "https://example.com/" Then
        Response.Redirect url
    End If
%>

迁移到现代技术

FromToWhy
ASP + VBScriptASP.NET Core + C#同一生态,性能与工具链好得多
ADOEntity Framework / Dapper参数化更自然,不易写错
FileSystemObjectSystem.IOUTF-8 原生支持,无 COM 开销
Application 缓存Redis / MemoryCache跨进程、跨机器共享
Global.asaProgram.cs 启动配置类型安全,可测试

Migration is not a rewrite. A common path keeps old pages and builds new features as separate services behind a reverse proxy on /api/*, shifting traffic gradually — every step shippable and reversible.

For an internal tool with a few dozen users, fixing security holes may beat migrating. Audit security first, then refactor — the reverse order often spends effort without solving the real problem.

继续学习

  1. SQL 与数据库设计 —— 索引、事务、范式,比语言本身更重要
  2. HTTP 与 Web 安全 —— OWASP Top 10 是必修
  3. 一门现代服务端语言 —— C#、Node.js 或 Go
  4. 前端工程化 —— 现代项目里服务端只返回数据

Friends & Partners