问题
In ADAL.NET 2.x, we use the below code to acquire token from Azure AD using UserCredential
and it works perfectly:
var authContext = new AuthenticationContext(Authority);
var userCredential = new UserCredential(username, password);
var token = authContext.AcquireToken(ResourceUrl, ClientId, userCredential);
When I upgraded ADAL.NET v3 today, the code cannot be compiled anymore because on the new version, UserCredential
does not have overloaded constructor with username and password.
How I can workaround this with the new version of ADAL.NET v3?
回答1:
Use UserPasswordCredential
class instead which is a subclass of UserCredential
回答2:
Try UserPasswordCredential
, the class had to be renamed in v3.
回答3:
FYI, it seems as though they have removed this functionality from ADAL. source
To authenticate with a users username/password combo, I believe you will have to use HttpClient and make the post request yourself.
Post to:
https://login.microsoftonline.com/yourdomain.onmicrosoft.com/oauth2/token
with:
resource={resource}&client_id={clientid}&grant_type=password&username={username}&password={password}&scope=openid&client_secret={clientsecret}
in the request
回答4:
This fixes the issue for the UserCredentials, but you also seems to be a change to the AuthenticationContext type which no longer seems to have an AcquireToken method. You can address this by using AcquireTokenAsync
来源:https://stackoverflow.com/questions/37465949/adal-net-v3-does-not-support-acquiretoken-with-usercredential