</>
Skip to content
ASP lessons (26/40)

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

PracticeDescription
Validate inputCheck all user data
Encode outputPrevent XSS
Use parameterized queriesPrevent SQL injection
Secure cookiesHttpOnly, Secure
Use HTTPSEncrypt traffic

Mini Practice

  1. Validate user input
  2. Prevent SQL injection
  3. Prevent XSS
  4. 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.