Showing posts with label Caching. Show all posts
Showing posts with label Caching. Show all posts

Tuesday, March 29, 2022

Azure @ Enterprise - How to cache JWT access token in MSAL.Net?

When we start using the MSAL.Net library to work with Azure Active Directory and experience its fluency and other features, we start thinking that has everything we need. If we take the simple example of obtaining a JWT access token, we naturally think that the library is caching the token based on the "exp" attribute. At least I came to multiple scenarios where developers thought, the library is caching access tokens but in reality it is not.

How to cache the JWT access tokens in MSAL.Net?

It is not easy and it is not the way we developers normally think a library will offer. 

First of all, we have to write code to get token from the cache. No, it won't return a null or empty string if the token is not present. Instead, it throws an exception. We have to catch that exception and acquire a token that the cache will intercept and update.
  • Try to get token from the cache using AcquireTokenSilent()
  • If it throws an exception try to get a token using other AcquireToken methods based on our authentication model.
This is not a workaround. It is the documented solution.

Monday, December 5, 2011

Successfully tried Windows Azure AppFabric Caching in Azure environment

You might have wondered why I am telling this as an achievement. It is meant to cache the data and what is there to say "Successfully tried". Yes.There are so many things to say about it even though there are so many articles which explain how to cache in a step by step manner with images. Roughly speaking it took me 3 days to test the caching in the real Azure hosted environment. Below are the issues we faced during the process.

Issue 1 : Wrong SDK version ie Azure SDK to Azure Guest OS mapping


Initially I was using Azure SDK 1.6 which is not supported in the Azure production servers. More details are explained here. Most of the time we were getting the DataCacheException with ErrorCode<ERRCA0017> which is nothing but RetryLater. Funny part is when we reverted to SDK 1.5 retry disappeared.

Issue 2 : Windows AppFabric v/s Azure AppFabric


Azure emulator don't have support to simulate Azure caching. So the idea was to use the Windows AppFabric cache which can be installed in our local machines and later change the configuration to use the actual Azure AppFabric .After configuring Windows AppFabric using Windows PowerShell and all we were able to connect to the local cache .But after hosting the same code to Azure, we came to know that there are some differences between Windows Server AppFabric and the Azure AppFabric. It made us rewrite our AppFabricCacheProvider class based on the Azure AppFabric caching API reference. The exceptions we got at this stage was NotSupportedException.
Below are some links which explains how to setup Windows Server AppFabric caching.
http://msdn.microsoft.com/en-us/library/ff637746.aspx
http://www.wadewegner.com/2010/08/getting-started-with-windows-server-appfabric-cache/

Issue 3 : Network issues to access caching clusters in your development environment


We tried to access the real Azure AppFabric cache from out development environment. The Azure caching machine was not reachable from our normal company network. My be some firewalls were in middle. We had resolved it using another external internet connection which don't have any restriction.
If you happen to face this issue, you will get exception which says it cannot access some particular IPAddress which you can easily verify by pinging or tracert

Issue 4 : AppFabric dlls to be included in the Azure publish package.


After 2 days we were able to successfully implement the caching framework and access Azure AppFabric cache from services in our development environment.Everybody became happy for some moment .But we lost the mood when we hosted our services in the Azure. Since we haven't implemented any logging framework it was really difficult for us to get the exact exception. The exception was mainly happening on the line .


DataCacheFactoryConfiguration = new DataCacheFactoryConfiguration("clientName");


Finally we were able to track that down to assembly not found issues ie "The system cannot find the file specified. Could not load file or assembly 'Microsoft.WindowsFabric.Common, Version=1.0.0.0" . Yes we were missing some dlls in the package which we uploaded to Azure. It was from the C:\Program Files\Windows Azure AppFabric SDK\V1.5\Assemblies\NET4.0\Cache folder.We added all the dlls, present in this location to our package and it stated working. Better refer these dlls in your web project / service host project so that these will be packaged automatically.

Microsoft.Web.DistributedCache.dll
Microsoft.WindowsFabric.Common.dll
Microsoft.WindowsFabric.Data.Common.dll

The question remains ,how it worked in the emulator? Answer was simple. All these dlls were in our GAC. in Azure machines, GAC don't have these dlls.

There were so many minor issues like the letter casing of <dataCacheClients>,should we use the <dataCacheClients> or <dataCacheClient> section in the config, port 22233 was blocked etc...Below are some links if you are struggling to get Azure AppFabric Caching working

http://blog.bareweb.eu/2011/07/hello-world-with-azure-appfabric-caching-service-v1-0/
Exercise 3: Creating a Reusable and Extensible Caching Layer
Appendix 1: Table of Error Code Strings