in src/DotNetWorker.ApplicationInsights/Initializers/TokenCredentialOptions.cs [36:104]
public static TokenCredentialOptions ParseAuthenticationString(string applicationInsightsAuthenticationString)
{
if (string.IsNullOrWhiteSpace(applicationInsightsAuthenticationString))
{
throw new ArgumentNullException(nameof(applicationInsightsAuthenticationString), "Authentication string cannot be null or empty.");
}
var tokenCredentialOptions = new TokenCredentialOptions();
bool isValidConfiguration = false;
foreach ((int, int) split in Tokenize(applicationInsightsAuthenticationString))
{
(int start, int length) = split;
var authenticationStringToken = applicationInsightsAuthenticationString
.AsSpan(start, length)
.Trim();
// Ignore (allow) empty tokens.
if (authenticationStringToken.IsEmpty)
{
continue;
}
// Find key-value separator.
int indexOfEquals = authenticationStringToken.IndexOf('=');
if (indexOfEquals < 0)
{
continue;
}
// Extract key
var key = authenticationStringToken[..indexOfEquals].TrimEnd();
if (key.IsEmpty)
{
// Key is blank
continue;
}
// check if the span matches the string "df":
if (key.CompareTo(AuthAuthorizationKey.AsSpan(), StringComparison.OrdinalIgnoreCase) == 0)
{
if (authenticationStringToken[(indexOfEquals + 1)..].CompareTo(AuthToken.AsSpan(), StringComparison.OrdinalIgnoreCase) != 0)
{
throw new InvalidCredentialException("Credential supplied is not valid for the authorization mechanism being used in ApplicationInsights.");
}
isValidConfiguration = true;
continue;
}
if (key.CompareTo(AuthClientIdKey.AsSpan(), StringComparison.OrdinalIgnoreCase) == 0)
{
var clientId = authenticationStringToken[(indexOfEquals + 1)..].Trim().ToString();
if (!Guid.TryParse(clientId, out Guid _))
{
throw new FormatException($"The Application Insights AuthenticationString {AuthClientIdKey} is not a valid GUID.");
}
tokenCredentialOptions.ClientId = clientId;
}
}
// Throw if the Authorization key is not present in the authentication string
if (!isValidConfiguration)
{
throw new InvalidCredentialException("Authorization key is missing in the authentication string for ApplicationInsights.");
}
return tokenCredentialOptions;
}