diff --git a/src/Accounts/Accounts/ChangeLog.md b/src/Accounts/Accounts/ChangeLog.md
index 3681cccc69..48a4cbc5ff 100644
--- a/src/Accounts/Accounts/ChangeLog.md
+++ b/src/Accounts/Accounts/ChangeLog.md
@@ -18,6 +18,7 @@
- Additional information about change #1
-->
## Upcoming Release
+* Display user friendly error message when context is null in Az 4.0
## Version 1.6.4
* Add a deprecation message for `Resolve-Error` alias.
diff --git a/src/Accounts/Accounts/CommonModule/ContextAdapter.cs b/src/Accounts/Accounts/CommonModule/ContextAdapter.cs
index 66b0e5d8ec..d3b0e2e513 100644
--- a/src/Accounts/Accounts/CommonModule/ContextAdapter.cs
+++ b/src/Accounts/Accounts/CommonModule/ContextAdapter.cs
@@ -170,6 +170,11 @@ namespace Microsoft.Azure.Commands.Common
///
internal async Task AuthorizeRequest(IAzureContext context, string resourceId, HttpRequestMessage request, CancellationToken outerToken)
{
+ if (context == null || context.Account == null || context.Environment == null)
+ {
+ throw new InvalidOperationException(Resources.InvalidAzureContext);
+ }
+
await Task.Run(() =>
{
resourceId = context?.Environment?.GetAudienceFromRequestUri(request.RequestUri) ?? resourceId;
diff --git a/src/Accounts/Accounts/Properties/Resources.Designer.cs b/src/Accounts/Accounts/Properties/Resources.Designer.cs
index de6395052f..98055436b6 100644
--- a/src/Accounts/Accounts/Properties/Resources.Designer.cs
+++ b/src/Accounts/Accounts/Properties/Resources.Designer.cs
@@ -483,6 +483,15 @@ namespace Microsoft.Azure.Commands.Profile.Properties {
}
}
+ ///
+ /// Looks up a localized string similar to The context is invalid. Please login using Connect-AzAccount..
+ ///
+ internal static string InvalidAzureContext {
+ get {
+ return ResourceManager.GetString("InvalidAzureContext", resourceCulture);
+ }
+ }
+
///
/// Looks up a localized string similar to Endpoint provided is invalid. Please check the value and retry again with the correct value..
///
diff --git a/src/Accounts/Accounts/Properties/Resources.resx b/src/Accounts/Accounts/Properties/Resources.resx
index a15186e7fc..e08a9558f6 100644
--- a/src/Accounts/Accounts/Properties/Resources.resx
+++ b/src/Accounts/Accounts/Properties/Resources.resx
@@ -465,4 +465,7 @@
A snapshot of the service API versiosn available in Azure Sovereign Clouds and the Azure Global Cloud.
+
+ The context is invalid. Please login using Connect-AzAccount.
+
\ No newline at end of file