Thanks for the help!
I have managed to connect via a Console App using the code below.
How can this be altered to work in a C# webservice?
using System;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
namespace Dynamics365ServerToServerAuthentication
{
class Program
{
static void Main(string[] args)
{
const string authorityUri = "login.microsoftonline.com/.../authorize";
const string d365Url = "xxxx.crm4.dynamics.com";
const string clientId = "0589294b-xxxxxxx";
const string clientKey = "0A27Qxxxxx";
ClientCredential clientCredentials = new ClientCredential(clientId, clientKey);
AuthenticationContext authenticationContext = new AuthenticationContext(authorityUri, false);
try
{
var authenticationResult = authenticationContext.AcquireTokenAsync(d365Url, clientCredentials).GetAwaiter().GetResult();
Console.WriteLine($"Connection success: {!string.IsNullOrEmpty(authenticationResult.AccessToken)}");
}
catch (Exception e)
{
Console.WriteLine(e);
}
Console.ReadKey();
}
}
}