Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…nto pr642
  • Loading branch information
jansenbe committed Jun 8, 2022
2 parents e3cbd5f + 6e2e6be commit f758cf3
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/lib/PnP.Framework/Extensions/ClientContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -635,13 +635,41 @@ 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<WebRequestEventArgs> handler = (s, e) =>
{
string authorization = e.WebRequestExecutor.RequestHeaders["Authorization"];
if (!string.IsNullOrEmpty(authorization))
{
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;
Expand Down

0 comments on commit f758cf3

Please sign in to comment.