5. Introducing token caching
Something we need to be aware of when we consume Azure Identity ourselves (i.e. outside of the Azure SDK), is that many concerns are not taken care of for us, like token caching.
In our previous implementation, we were requesting a token (implying a network request) every time we load the page, which is far from ideal.
To remediate this, we take the following steps:
- We create a new abstraction called
IAzureSqlTokenProvider
representing the process of acquiring a token for Azure SQL. - We create a base implementation that leverages Azure Identity, which is identical to what we had before in
SqlConnectionFactory
. - We introduce caching via the decorator pattern; to avoid network requests, we cache them in memory.
- Finally, we use the excellent
Scrutor
NuGet package that adds support for decorators to the built-in dependency injection container.
Diff from previous tag: 4.using-sql-with-aad-auth...5.introduce-token-caching