First consider the following code snippet generated by ASP.NET Core MVC scaffolding.
// GET: Students/Delete/5
public async Task<IActionResult> Delete(int? id)
{
if (id == null)
{
return NotFound();
}
var student = await _context.Students
.SingleOrDefaultAsync(m => m.ID == id);
if (student == null)
{
return NotFound();
}
return View(student);
}
// POST: Students/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(int id)
{
var student = await _context.Students.SingleOrDefaultAsync(m => m.ID == id);
_context.Students.Remove(student);
await _context.SaveChangesAsync();
return RedirectToAction("Index");
}
There are some differences in HttpGet
and HttpPost
action methods as follows:
id
is nullable in Get but not nullable in Post.- The preliminary check as follows is only in Get.
Code:
if (id == null)
{
return NotFound();
}
var student = await _context.Students
.SingleOrDefaultAsync(m => m.ID == id);
if (student == null)
{
return NotFound();
}
Questions:
For example, the visitor request id=5
to be deleted in GET but later he tampers with the id
in POST by setting it to a number such as id=6
or setting it to an invalid value such as id=xodsfsdofsdfosdfsd
. As there is no preliminary checks in HttpPost
, how to prevent this?