diff --git a/src/lib/PnP.Framework/Extensions/ClientContextExtensions.cs b/src/lib/PnP.Framework/Extensions/ClientContextExtensions.cs index 927426c74..6c78860e4 100644 --- a/src/lib/PnP.Framework/Extensions/ClientContextExtensions.cs +++ b/src/lib/PnP.Framework/Extensions/ClientContextExtensions.cs @@ -635,6 +635,29 @@ public static string GetAccessToken(this ClientRuntimeContext clientContext) } else { + // Get User Agent String + string userAgentFromConfig = null; + try + { + userAgentFromConfig = ConfigurationManager.AppSettings["SharePointPnPUserAgent"]; + } + catch // throws exception if being called from a .NET Standard 2.0 application + { + + } + + // Get user Agent String if being called from a .NET Standard 2.0 application or is missing + if (string.IsNullOrWhiteSpace(userAgentFromConfig)) + { + userAgentFromConfig = Environment.GetEnvironmentVariable("SharePointPnPUserAgent", EnvironmentVariableTarget.Process); + } + + // Use Default User Agent String + if (string.IsNullOrWhiteSpace(userAgentFromConfig)) + { + userAgentFromConfig = PnPCoreUtilities.PnPCoreUserAgent; + } + EventHandler handler = (s, e) => { string authorization = e.WebRequestExecutor.RequestHeaders["Authorization"]; @@ -642,6 +665,11 @@ public static string GetAccessToken(this ClientRuntimeContext clientContext) { accessToken = authorization.Replace("Bearer ", string.Empty); } + + if (!string.IsNullOrWhiteSpace(userAgentFromConfig)) + { + e.WebRequestExecutor.WebRequest.Headers.Add("User-Agent", userAgentFromConfig); + } }; // Issue a dummy request to get it from the Authorization header clientContext.ExecutingWebRequest += handler;