- Lab
- Security

Sanitize User Input for C# Applications
In this lab, you will practice securing web applications against SQL Injection, XSS, and CSRF attacks. When you finish, you will have a fully functional ASP.NET Core MVC application with implemented protections for each vulnerability.

Path Info
Table of Contents
-
Challenge
Getting Started in the Lab Environment
Here are the initial instructions and explanation of the lab environment. Read this while your environment is busy creating itself from nothing. Yes, this violates physics; we know. How fun!
-
Challenge
Sanitize User Input for C#: Concept Review
You'll review core web vulnerabilities: SQL Injection, XSS, and CSRF. Learn how untrusted input, unencoded output, and missing request validation expose risks—and how to mitigate them in ASP.NET Core MVC apps.
-
Challenge
SQL Injection Vulnerability and Mitigation
You'll analyze a C# console app with a SQL injection vulnerability caused by string concatenation. You'll then refactor it using parameterized queries with SqlCommand and test it with both valid and malicious inputs to confirm the fix.
-
Challenge
Cross-Site Scripting (XSS) Vulnerability and Prevention
You'll build a Razor view in ASP.NET Core MVC that initially renders unencoded input, exposing an XSS risk. You'll then apply WebUtility.HtmlEncode and test with script payloads to confirm the secure version renders the input as harmless text.
-
Challenge
Cross-Site Request Forgery (CSRF) Prevention
You'll create an ASP.NET Core MVC app with a state-changing form that lacks CSRF protection. You'll then add anti-forgery tokens and apply the ValidateAntiForgeryToken attribute to secure it and ensure POST requests come from trusted sources.
-
Challenge
Knowledge Check for Sanitize User Input for C# Applications
In this challenge, you will answer some assessment questions that will test your understanding of the concepts you learned throughout this lab.
What's a lab?
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Provided environment for hands-on practice
We will provide the credentials and environment necessary for you to practice right within your browser.
Guided walkthrough
Follow along with the author’s guided walkthrough and build something new in your provided environment!
Did you know?
On average, you retain 75% more of your learning if you get time for practice.