Microsoft.IdentityModel.Clients.ActiveDirectory is deprecated, use Microsoft.Identity.Client

4.3k Views Asked by At

I am using Microsoft.IdentityModel.Clients.ActiveDirectory NuGet and for an app I retrieve token like below code:

public async Task<IotHubClient> GetIotHubClient()
{
    var authContext = new AuthenticationContext(_configuration["IoTHub:Credentials:Authority"]);
    var credential = new ClientCredential(_configuration["IoTHub:Credentials:ClientId"], await _secretKeyReader.GetSecretValue("IotHubScalingAppKey"));
    var token = await authContext.AcquireTokenAsync(_configuration["IoTHub:Credentials:Resource"], credential);

    if (token == null) return null;

    var credentials = new TokenCredentials(token.AccessToken);
    var client = new IotHubClient(credentials)
    {
        SubscriptionId = _configuration["IoTHub:Credentials:SubscriptionId"]
    };

    return client;
}

Since the package Microsoft.IdentityModel.Clients.ActiveDirectory is deprecated, I'm trying to use the package Microsoft.Identity.Client, but the above method no longer works.

What's the way to get the token?

0

There are 0 best solutions below