In ADAL.Net 3.x UserPasswordCredential is introduced on top of UserCredential from 2.x. But the same UserPasswordCredential is not exposed in the .Net Core under the same nuget package?
UserCredential class has only one property UserName
namespace Microsoft.IdentityModel.Clients.ActiveDirectory
{
//
// Summary:
// Credential used for integrated authentication on domain-joined machines.
public class UserCredential
{
//
// Summary:
// Constructor to create user credential. Using this constructor would imply integrated
// authentication with logged in user and it can only be used in domain joined scenarios.
public UserCredential();
//
// Summary:
// Constructor to create credential with client id and secret
//
// Parameters:
// userName:
// Identifier of the user application requests token on behalf.
public UserCredential(string userName);
//
// Summary:
// Gets identifier of the user.
public string UserName { get; }
}
}
Since UserPasswordCredential is not available in .NetCore and UserCredential takes only one parameter username, how to input the password of the user and implement below code in .Net Core?
authContext.AcquireTokenAsync(WebAPIResourceId, ClientId, userPasswordCredential);
I am using ADAL 3.13.4 version specifically in .Net Core 1.0 version
Below is what i have been doing to get around this problem. I replicated the same behaviour in a static method for use in .NET Core, since the UserPasswordCredential class is missing. This is based on fiddler traces of what happens when the UserPasswordCredential class is used in the .NET version. Since the .NET DLL seems to be obfuscated, this is a best attempt at capturing what it does.
You are correct,
UserPasswordCredential
is not available for .NET Core, andUserCredential
no longer accepts username and password. This means ADAL v3 does not support the username/password flow on .NET Core.Fast forward to 2020, with ADAL 3.19.8, you should be able to use the ClientCredential class for AAD authentication. It is working for me when integrating with a D365 CRM web API. I documented my experience in the following blog post. Hope you will find it useful.
To use the resource owner password credentials grant flow to get the access token for Azure AD, we can call the http request diectly using the HttpClient. Here is an example for your reference :