ASP — Security
Input Validation
<%
Dim username
username = Request.Form("username")
' Validate
If Len(username) = 0 Then
Response.Write("Username required")
End If
%>
SQL Injection Prevention
<%
' Bad - vulnerable to SQL injection
sql = "SELECT * FROM users WHERE name = '" & username & "'"
' Good - use parameterized queries
Set cmd = Server.CreateObject("ADODB.Command")
cmd.CommandText = "SELECT * FROM users WHERE name = ?"
cmd.Parameters.Append cmd.CreateParameter("name", 200, 1, 50, username)
%>
XSS Prevention
<%
' Encode output
Response.Write(Server.HTMLEncode(userInput))
%>
Session Security
<%
' Regenerate session ID
Session.Abandon
Session("authenticated") = True
' Set secure cookie
Response.Cookies("session").Secure = True
Response.Cookies("session").HttpOnly = True
%>
Security Checklist
| Practice | Description |
|---|---|
| Validate input | Check all user data |
| Encode output | Prevent XSS |
| Use parameterized queries | Prevent SQL injection |
| Secure cookies | HttpOnly, Secure |
| Use HTTPS | Encrypt traffic |
Mini Practice
- Validate user input
- Prevent SQL injection
- Prevent XSS
- Secure session cookies
Up Next
Continue with Error Handling — error handling.
Related Topics
Frequently Asked Questions about Security
What is Security in ASP?
Security is a fundamental concept in ASP. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Security?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Security.
Why is Security important in ASP?
Security is essential for ASP development. Understanding this concept will help you write better code and solve real-world problems more effectively.