Getting Identity Claims to update immediately

Asp.Net Identity Claims are a very useful feature for adding extra metadata to a user's account. This metadata could consist of a user's organization, affiliation, or even profile settings that they can set through the app.

In the latter case of having claims values that can be updated dynamically, you will want to have the changes to these values reflected as soon as possible to avoid potential UX issues.

By default, claims are refreshed when a user logs into the app. In the case of users who stay logged in for prolonged periods, we want to have control over the interval for when the claims cookie gets refreshed from the database.

The default interval that Identity sets for you is not ideal for dynamically updated claims values. It is located in Startup.Auth.cs (assuming this is standard .Net MVC project).




As you can see in the screen above, the default is set to 30 min. To configure claims values to update more quickly, just change the Timespan to something like...

 validateInterval: TimeSpan.FromMinutes(5),

or even

validateInterval: TimeSpan.FromSeconds(30),


One thing to keep in mind, lowering the cache too much can cause extra database hits as users make requests to the app.

Hope this helps!

Comments

Popular posts from this blog

ASP.NET Identity Remember Me

IIS Express Client Certificates

ASP.NET MVC - How to enable/disable CaC/Client Certificate authentication per area or route.