Hi There
I had to rewrite these things for us when MS withdrew the old Protocol for connecting. You also have to ensure that you get the ClientID and redirecturl set corectly
The example I use goes like this
string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["Connect"].ConnectionString;
using (HttpClient client = EnhancedQuickStart.SampleHelpers.GetHttpClient(connectionString, EnhancedQuickStart.SampleHelpers.clientId, EnhancedQuickStart.SampleHelpers.redirectUrl))
{
// -------------------------------------------
// Put your stuff here
// -------------------------------------------
}
The Connection String looks like this
<add name="Connect" connectionString="Url=https://XXXXXXX.api.crm??.dynamics.com;Username=*****@*****;Password=**;" />
The Components that I store in a separate class look like this
public static string clientId = "******************************";
public static string redirectUrl = "app://******************************";
/// <summary>
/// Method used to get a value from the connection string
/// </summary>
/// <param name="connectionString"></param>
/// <param name="parameter"></param>
/// <returns>The value from the connection string that matches the parameter key value</returns>
public static string GetParameterValueFromConnectionString(string connectionString, string parameter)
{
try
{
return connectionString.Split(';').Where(s => s.Trim().StartsWith(parameter)).FirstOrDefault().Split('=')[1];
}
catch (Exception)
{
return string.Empty;
}
}
/// <summary>
/// Returns an HttpClient configured with an OAuthMessageHandler
/// </summary>
/// <param name="connectionString">The connection string to use.</param>
/// <param name="clientId">The client id to use when authenticating.</param>
/// <param name="redirectUrl">The redirect Url to use when authenticating</param>
/// <param name="version">The version of Web API to use. Defaults to version 9.2 </param>
/// <returns>An HttpClient you can use to perform authenticated operations with the Web API</returns>
public static HttpClient GetHttpClient(string connectionString, string clientId, string redirectUrl, string version = "v9.2")
{
string url = GetParameterValueFromConnectionString(connectionString, "Url");
string username = GetParameterValueFromConnectionString(connectionString, "Username");
string password = GetParameterValueFromConnectionString(connectionString, "Password");
try
{
HttpMessageHandler messageHandler = new OAuthMessageHandler(url, clientId, redirectUrl, username, password,
new HttpClientHandler());
HttpClient httpClient = new HttpClient(messageHandler)
{
BaseAddress = new Uri(string.Format("{0}/api/data/{1}/", url, version)),
Timeout = new TimeSpan(0, 2, 0) //2 minutes
};
return httpClient;
}
catch (Exception)
{
throw;
}
}
/// <summary> Displays exception information to the console. </summary>
/// <param name="ex">The exception to output</param>
public static void DisplayException(Exception ex)
{
Console.WriteLine("The application terminated with an error.");
Console.WriteLine(ex.Message);
while (ex.InnerException != null)
{
Console.WriteLine("\t* {0}", ex.InnerException.Message);
ex = ex.InnerException;
}
}
private AuthenticationHeaderValue authHeader;
public OAuthMessageHandler(string serviceUrl, string clientId, string redirectUrl, string username, string password,
HttpMessageHandler innerHandler)
: base(innerHandler)
{
string apiVersion = "9.2";
string webApiUrl = $"{serviceUrl}/api/data/v{apiVersion}/";
//Build Microsoft.Identity.Client (MSAL) OAuth Token Request
var authBuilder = PublicClientApplicationBuilder.Create(clientId)
.WithAuthority(AadAuthorityAudience.AzureAdMultipleOrgs)
.WithRedirectUri(redirectUrl)
.Build();
var scope = serviceUrl + "//.default";
string[] scopes = { scope };
AuthenticationResult authBuilderResult;
if (username != string.Empty && password != string.Empty)
{
//Make silent Microsoft.Identity.Client (MSAL) OAuth Token Request
var securePassword = new SecureString();
foreach (char ch in password) securePassword.AppendChar(ch);
authBuilderResult = authBuilder.AcquireTokenByUsernamePassword(scopes, username, securePassword)
.ExecuteAsync().Result;
}
else
{
//Popup authentication dialog box to get token
authBuilderResult = authBuilder.AcquireTokenInteractive(scopes)
.ExecuteAsync().Result;
}
//Note that an Azure AD access token has finite lifetime, default expiration is 60 minutes.
authHeader = new AuthenticationHeaderValue("Bearer", authBuilderResult.AccessToken);
}
protected override Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
{
request.Headers.Authorization = authHeader;
return base.SendAsync(request, cancellationToken);
}