Problem
How to prevent insecure direct object reference in ASP.NET Core.
Solution
Create an empty project and update the Startup class.
- public void ConfigureServices(
- IServiceCollection services)
- {
- services.AddMvc();
- services.AddDataProtection();
- }
- public void Configure(
- IApplicationBuilder app,
- IHostingEnvironment env)
- {
- app.UseMvcWithDefaultRoute();
- }
Create a model.
- public class Movie
- {
- public int Id { get; set; }
- public string Title { get; set; }
- public int ReleaseYear { get; set; }
- public string Summary { get; set; }
- }
Create a controller and inject IDataProtectionProvider as dependency.
- [Route("movies")]
- public class MoviesController : Controller
- {
- private readonly IDataProtector protector;
- public MoviesController(IDataProtectionProvider provider)
- {
- this.protector = provider.CreateProtector("protect_my_query_string");
- }
- ...
Add a method that retrieves the data and then, encrypt the object reference (Id property here).
- [HttpGet]
- public IActionResult Get()
- {
- var model = GetMovies(); // simulate call to repository
- var outputModel = model.Select(item => new
- {
- Id = this.protector.Protect(item.Id.ToString()),
- item.Title,
- item.ReleaseYear,
- item.Summary
- });
- return Ok(outputModel);
- }
Add a method that receives the encrypted object reference and then decrypts it.
- [HttpGet("{id}")]
- public IActionResult Get(string id)
- {
- var orignalId = int.Parse(this.protector.Unprotect(id));
- var model = GetMovies(); // simulate call to repository
- var outputModel = model.Where(item => item.Id == orignalId);
- return Ok(outputModel);
- }
Running the sample (browsing to /movies) with show encrypted references.

Discussion
OWASP 2013 classifies Insecure Direct Object Reference as one of the top 10 risks and is present if the object references (e.g. primary key of a database record) can be manipulated for malicious attacks.
One possible method to prevent is shown in the example above, i.e. by encrypting the internal references we can hide the internal details of our database/application structure. We can encrypt in various ways, here I am using the new Data Protection API in ASP.NET Core.
Data Protection API
There are two key abstractions we utilize to encrypt the data, IDataProtectionProvider and IDataProtector. We use the provider to create a protector by calling its CreateProtector() method. This method takes in a string key (known as Purpose String). Once we have a protector, we can use the Protect() method to encrypt and the Unprotect() method to decrypt the data.
Purpose Strings
It’s a key that ensures isolation between different protectors (cryptographic consumers) i.e. data encrypted by protector A cannot be read by a protector B, as long as they use different purpose strings.
Exception
Trying to decrypt the data that has been modified will throw CryptographicException,


Join the conversation! Your thoughts help the community grow.