I'm building a small service fabric maintenance in aspnetcore (dotnetcore2.0) application but now doesn't recognize the UserPasswordCredential class. From here it is by design as they say. With that I need to convert my previous code so that I can create a valid FabricClient. How I can convert the GetAccessToken() method?
private static FabricClient CreateSecuredFabricClient()
{
try
{
var claimsCredentials = new ClaimsCredentials();
claimsCredentials.ServerThumbprints.Add(_sslThumbprint);
var fc = new FabricClient(claimsCredentials, _clusterConnectionString);
fc.ClaimsRetrieval += (o, e) =>
{
var token = GetAccessToken(e.AzureActiveDirectoryMetadata);
return token.Result.AccessToken;
};
return fc;
}
catch (FabricException fex)
{
Console.WriteLine("Connect failed: {0}", fex.InnerException.Message);
}
catch (Exception e)
{
Console.WriteLine("Connect failed: {0}", e.Message);
}
return null;
}
private static Task<AuthenticationResult> GetAccessToken(System.Fabric.Security.AzureActiveDirectoryMetadata aad)
{
var authContext = new AuthenticationContext(aad.Authority);
var authResult = authContext.AcquireTokenAsync(
aad.ClusterApplication,
aad.ClientApplication,
new UserPasswordCredential(_username, _password));
return authResult;
}
Here's an example of how to authenticate to AAD with a clientid & clientsecret, by using ADAL.
(this works in aspnetcore 2.0 as well)
Essential code: