Business Central allows us to perform Isolated Storage, which is extremely useful if, for example, we want to store Passwords, Tokes, and Secrets without having to resort to Tables.
A couple of weeks ago I made a post on How to Test Outh2, through the native Business Central codeunit, but every time we closed the window, the token disappeared. So I came up with a good example idea, adding Isolate Storage to mitigate this issue.
Project Overview
Basic concepts
IsolatedStorage Data Type
- Version: Available or changed with runtime version 2.0.
Provides data isolation for extensions.
The following methods are available on the IsolatedStorage data type.
Method name | Description |
---|---|
Contains(Text [, DataScope]) | Determines whether the storage contains a value with the specified key. |
Delete(Text [, DataScope]) | Deletes the value with the specified key from the isolated storage. |
Get(Text [, DataScope], var Text) | Gets the value associated with the specified key. |
Get(Text, var Text) | Gets the value associated with the specified key. |
Set(Text, Text [, DataScope]) | Sets the value associated with the specified key. |
SetEncrypted(Text, Text [, DataScope]) | Encrypts and sets the value associated with the specified key. The input string cannot exceed a length of 215 plain characters; be aware that special characters take up more space. |
DataScope Option Type
- Version: Available or changed with runtime version 2.0.
Identifies the scope of stored data in the isolated storage.
Members
Member | Description |
---|---|
Module | Indicates that the record is available in the scope of the app(extension) context. |
Company | Indicates that the record is available in the scope of the company within the app context. |
User | Indicates that the record is available for a user within the app context. |
CompanyAndUser | Indicates that the record is available for a user and specific company within the app context. |
Business Central
The following code shows a very simple way of using Isolate Storage to store and retrieve information.
Based on the above, I have decided to modify the OpenPage Trigger to read if there is any stored token.
And I have placed the ‘set method’ of Isolated Storage inside the method that allows us to obtain a new token, if and only if it happens that there is no Token, or that the Token could not be refreshed.
Putting it all together.
Test
To run these tests, the first thing I will do is open the OAuth2 Test page.
When opening it, as it is the first time, we can see that the tokens are empty.
Next, I’m going to Make the Token call for BC APIs and then for the Graph API.
Getting Graph API Token
Getting Graph API Business Central
After closing the Test Oauth2 window, and reopening it we can see that the Token automatically appears.
In this way, thanks to the Isolate Storage, we are minimizing the calls to obtain a new token, as long as it can also be refreshed.
Conclusion
With Isolate Storage we can support ourselves enormously, to store keys, secrets, and certificates, that we need to manage within an extension and that we could in turn use to access from another extension.
Isolate Storage, could be very useful for example, to store all the configurations necessary for Outh2 authentication, for the latter, there are Azure Key Vaults, which I would like to talk a little about in my next post.
Code on GitHub
All the code used in this post can be found at the following link:
For more information, you can visit my previous post or the official documentation of the use of the library exposed in this post.
I hope this has been helpful.