diff --git a/msrestazure/azure_active_directory.py b/msrestazure/azure_active_directory.py
index 48ac41c..88c3744 100644
--- a/msrestazure/azure_active_directory.py
+++ b/msrestazure/azure_active_directory.py
@@ -685,6 +685,8 @@ class MSIAuthentication(BasicTokenAuthentication):
         elif "MSI_ENDPOINT" not in os.environ:
             # Use IMDS if no MSI_ENDPOINT
             self._vm_msi = _ImdsTokenProvider(self.resource, self.msi_conf)
+        # Follow the same convention as all Credentials class to check for the token at creation time #106
+        self.set_token()
 
     def set_token(self):
         if _is_app_service():

MSIAuthentication should set toekn on instance creation
All msrestazure instance set the token on instance creation, except MSIAuthentication.

This presents a few issues:
- Unlike all other usages, authentication exception will be raised at usage, not at creation level
- This creates weird bug in KeyVault which is doing some magic inside credentials:
https://github.com/Azure/azure-sdk-for-python/issues/2852
diff --git a/tests/test_auth.py b/tests/test_auth.py
index ed5ae77..56baf9c 100644
--- a/tests/test_auth.py
+++ b/tests/test_auth.py
@@ -240,7 +240,7 @@ class TestServicePrincipalCredentials(unittest.TestCase):
             ServicePrincipalCredentials, '_setup_session', return_value=session):
 
             proxies = {'http': 'http://myproxy:80'}
-            creds = ServicePrincipalCredentials("client_id", "secret", 
+            creds = ServicePrincipalCredentials("client_id", "secret",
                                                 verify=False, tenant="private",
                                                 proxies=proxies)
 
@@ -311,7 +311,7 @@ class TestServicePrincipalCredentials(unittest.TestCase):
             UserPassCredentials, '_setup_session', return_value=session):
 
             proxies = {'http': 'http://myproxy:8080'}
-            creds = UserPassCredentials("my_username", "my_password", 
+            creds = UserPassCredentials("my_username", "my_password",
                                         verify=False, tenant="private", resource='resource',
                                         proxies=proxies)
 
@@ -381,7 +381,7 @@ class TestServicePrincipalCredentials(unittest.TestCase):
         assert token_type == "TokenType"
         assert access_token == "AccessToken"
         assert token_entry == json_payload
-        
+
         httpretty.register_uri(httpretty.POST,
                                'http://localhost:42/oauth2/token',
                                status=503,
@@ -419,10 +419,9 @@ class TestServicePrincipalCredentials(unittest.TestCase):
                                content_type="application/json")
 
         credentials = MSIAuthentication()
-        credentials.set_token()
         assert credentials.scheme == "TokenTypeIMDS"
         assert credentials.token == json_payload
-        
+
         # Test MSIAuthentication with MSI_ENDPOINT and no APPSETTING_WEBSITE_SITE_NAME is MSI_ENDPOINT
 
         json_payload = {
@@ -436,7 +435,6 @@ class TestServicePrincipalCredentials(unittest.TestCase):
 
         with mock.patch('os.environ', {'MSI_ENDPOINT': 'http://random.org/yadadada'}):
             credentials = MSIAuthentication()
-            credentials.set_token()
             assert credentials.scheme == "TokenTypeMSI_ENDPOINT"
             assert credentials.token == json_payload
 
@@ -458,14 +456,13 @@ class TestServicePrincipalCredentials(unittest.TestCase):
         }
         with mock.patch.dict('os.environ', app_service_env):
             credentials = MSIAuthentication(resource="foo")
-            credentials.set_token()
             assert credentials.scheme == "TokenTypeWebApp"
             assert credentials.token == json_payload
 
 
     @httpretty.activate
     def test_msi_vm_imds_retry(self):
- 
+
         json_payload = {
             'token_type': "TokenTypeIMDS",
             "access_token": "AccessToken"
@@ -484,20 +481,18 @@ class TestServicePrincipalCredentials(unittest.TestCase):
                                body=json.dumps(json_payload),
                                content_type="application/json")
         credentials = MSIAuthentication()
-        credentials.set_token()
         assert credentials.scheme == "TokenTypeIMDS"
         assert credentials.token == json_payload
 
 
     @httpretty.activate
     def test_msi_vm_imds_no_retry_on_bad_error(self):
- 
+
         httpretty.register_uri(httpretty.GET,
                                'http://169.254.169.254/metadata/identity/oauth2/token',
                                status=499)
-        credentials = MSIAuthentication()
         with self.assertRaises(HTTPError) as cm:
-            credentials.set_token()
+            credentials = MSIAuthentication()
 
 
 @pytest.mark.slow
@@ -515,7 +510,7 @@ def test_refresh_userpassword_no_common_session(user_password):
     # Hacking the token time
     creds.token['expires_on'] = time.time() - 10
     creds.token['expires_at'] = creds.token['expires_on']
-    
+
     try:
         session = creds.signed_session()
         response = session.get("https://management.azure.com/subscriptions?api-version=2016-06-01")
@@ -541,7 +536,7 @@ def test_refresh_userpassword_common_session(user_password):
     # Hacking the token time
     creds.token['expires_on'] = time.time() - 10
     creds.token['expires_at'] = creds.token['expires_on']
-    
+
     try:
         session = creds.signed_session(root_session)
         response = session.get("https://management.azure.com/subscriptions?api-version=2016-06-01")
@@ -573,7 +568,7 @@ def test_refresh_aadtokencredentials_no_common_session(user_password):
     # Hacking the token time
     creds.token['expires_on'] = time.time() - 10
     creds.token['expires_at'] = creds.token['expires_on']
-    
+
     try:
         session = creds.signed_session()
         response = session.get("https://management.azure.com/subscriptions?api-version=2016-06-01")
@@ -607,7 +602,7 @@ def test_refresh_aadtokencredentials_common_session(user_password):
     # Hacking the token time
     creds.token['expires_on'] = time.time() - 10
     creds.token['expires_at'] = creds.token['expires_on']
-    
+
     try:
         session = creds.signed_session(root_session)
         response = session.get("https://management.azure.com/subscriptions?api-version=2016-06-01")

pip install -e .[dev]
parse_log_pytest
requirements.txt
mock httpretty pytest pytest-cov pylint
apt-get update apt-get install -y gcc libunwind8-dev
3.6
dev_requirements.txt
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
adal==1.2.7
astroid==2.11.7
attrs==22.2.0
azure-core==1.24.2
certifi==2021.5.30
cffi==1.15.1
charset-normalizer==2.0.12
coverage==6.2
cryptography==40.0.2
dill==0.3.4
httpretty==1.1.4
idna==3.10
importlib-metadata==4.8.3
iniconfig==1.1.1
isodate==0.6.1
isort==5.10.1
jeepney==0.7.1
keyring==23.4.1
lazy-object-proxy==1.7.1
mccabe==0.7.0
mock==5.2.0
msrest==0.7.1
-e git+https://github.com/Azure/msrestazure-for-python.git@bc59bae35d784f0aad8d1e26ebfd9c20897dca46#egg=msrestazure
oauthlib==3.2.2
packaging==21.3
platformdirs==2.4.0
pluggy==1.0.0
py==1.11.0
pycparser==2.21
PyJWT==2.4.0
pylint==2.13.9
pyparsing==3.1.4
pytest==7.0.1
pytest-cov==4.0.0
python-dateutil==2.9.0.post0
requests==2.27.1
requests-oauthlib==2.0.0
SecretStorage==3.3.3
six==1.17.0
tomli==1.2.3
typed-ast==1.5.5
typing_extensions==4.1.1
urllib3==1.26.20
wrapt==1.16.0
zipp==3.6.0

name: msrestazure-for-python
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - ca-certificates=2025.2.25=h06a4308_0
  - certifi=2021.5.30=py36h06a4308_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.3=he6710b0_2
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - ncurses=6.4=h6a678d5_0
  - openssl=1.1.1w=h7f8727e_0
  - pip=21.2.2=py36h06a4308_0
  - python=3.6.13=h12debd9_1
  - readline=8.2=h5eee18b_0
  - setuptools=58.0.4=py36h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - wheel=0.37.1=pyhd3eb1b0_0
  - xz=5.6.4=h5eee18b_1
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - adal==1.2.7
      - astroid==2.11.7
      - attrs==22.2.0
      - azure-core==1.24.2
      - cffi==1.15.1
      - charset-normalizer==2.0.12
      - coverage==6.2
      - cryptography==40.0.2
      - dill==0.3.4
      - httpretty==1.1.4
      - idna==3.10
      - importlib-metadata==4.8.3
      - iniconfig==1.1.1
      - isodate==0.6.1
      - isort==5.10.1
      - jeepney==0.7.1
      - keyring==23.4.1
      - lazy-object-proxy==1.7.1
      - mccabe==0.7.0
      - mock==5.2.0
      - msrest==0.7.1
      - oauthlib==3.2.2
      - packaging==21.3
      - platformdirs==2.4.0
      - pluggy==1.0.0
      - py==1.11.0
      - pycparser==2.21
      - pyjwt==2.4.0
      - pylint==2.13.9
      - pyparsing==3.1.4
      - pytest==7.0.1
      - pytest-cov==4.0.0
      - python-dateutil==2.9.0.post0
      - requests==2.27.1
      - requests-oauthlib==2.0.0
      - secretstorage==3.3.3
      - six==1.17.0
      - tomli==1.2.3
      - typed-ast==1.5.5
      - typing-extensions==4.1.1
      - urllib3==1.26.20
      - wrapt==1.16.0
      - zipp==3.6.0
prefix: /opt/conda/envs/msrestazure-for-python

tests/test_auth.py::TestServicePrincipalCredentials::test_msi_vm tests/test_auth.py::TestServicePrincipalCredentials::test_msi_vm_imds_no_retry_on_bad_error tests/test_auth.py::TestServicePrincipalCredentials::test_msi_vm_imds_retry
tests/test_auth.py::TestServicePrincipalCredentials::test_adal_authentication tests/test_auth.py::TestServicePrincipalCredentials::test_check_state tests/test_auth.py::TestServicePrincipalCredentials::test_clear_token tests/test_auth.py::TestServicePrincipalCredentials::test_convert_token tests/test_auth.py::TestServicePrincipalCredentials::test_credentials_get_stored_auth tests/test_auth.py::TestServicePrincipalCredentials::test_credentials_retrieve_session tests/test_auth.py::TestServicePrincipalCredentials::test_http tests/test_auth.py::TestServicePrincipalCredentials::test_https tests/test_auth.py::TestServicePrincipalCredentials::test_service_principal tests/test_auth.py::TestServicePrincipalCredentials::test_store_token tests/test_auth.py::TestServicePrincipalCredentials::test_store_token_boom tests/test_auth.py::TestServicePrincipalCredentials::test_user_pass_credentials
msrestazure/azure_active_directory.py
msrestazure/azure_active_directory.py