Office 365 - How to fix Microsoft.Graph.ServiceException: Code: generalException System.Security.Cryptography.CryptographicException: Key not valid for use in specified state.

Symptom:
Activities inside Office 365 scope fail with Microsoft.Graph.ServiceException “Key not valid for use in specified state”.
Authentication with User-Pass or Interactive is passed, but the activity inside fails, be it Send Mail or Get Mail.

Specific scenario:
We have cloned our virtual machine production terminal server robot station. We have the same users and the same folder permissions and everything is the same on the new server.
The Office 365 activities on the production server work perfectly.
The Office 365 activities on the newly cloned server (Dev server) fail with the error above.

Cause:
Some cache files have remained in the robot accounts directories from the production server used by UiPath. The cached files contain some tokens or keys used by the MSAL protocols in UiPath implementation of O365 encrypted by the production server. The same keys or tokens cannot be decrypted properly by the current machine (Dev server), apparently because of different hardware ids used in the encryption/decryption.

Solution:
Delete the Cached file “office365.tokens.msalcache.bin3” from directory
“%AppData%UiPath\authentication” or in our case
“C:\Users\USERNAME\AppData\Roaming\UiPath\authentication”
The file is created by the process at first use.

Note:
We have deleted any file we found there anyways, as it may interfere with something else.

1 Like

Thanks @vladimir.stoyanov it works like a charm, we’ve built a workaround… Just thinking, should it be taken as an activities/libs bug category?
Hug!

Hey, Jorge! Very glad it helped.
I don’t think it is a bug, as ours is a special case and once fixed this way it works as it never happened…
If you are willing to report it as a bug, please be my guest, but I have spent enough time already and have moved on to another topic :slight_smile:
Cheers!

1 Like