17

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?

cuongle
  • 74,024
  • 28
  • 151
  • 206

4 Answers4

26

Use UserPasswordCredential class instead which is a subclass of UserCredential

cuongle
  • 74,024
  • 28
  • 151
  • 206
Kanishk Panwar
  • 1,105
  • 8
  • 7
  • I get AADSTS70002: The request body must contain the following parameter: 'client_secret or client_assertion'. Trace ID: 8be09594-65c2-4e2d-bce2-17980f739371 Correlation ID: 7312b657-f6d4-4d0b-8fc2-c66f1b9f0325 Timestamp: 2016-12-28 16:14:16Z – Russell at ISC Dec 28 '16 at 16:15
  • you are trying to use confidential client (web api) id for a native application. You need to create a new native app id for your flow – Kanishk Panwar Dec 29 '16 at 17:15
  • Hey im using this and in debug configuration it works fine but in release i get AADSTS50126: Invalid username or password even though im using the same password username, any idea what could cause this? – ZoomVirus Feb 21 '17 at 16:16
3

Try UserPasswordCredential, the class had to be renamed in v3.

dstrockis
  • 1,173
  • 5
  • 6
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

wh-dev
  • 537
  • 5
  • 18
  • What is the value for "resource" parameter in the request body? I am guessing but it does not work. – Sameeksha Kumari Jun 08 '18 at 09:00
  • 1
    Hi @SameekshaKumari, this should answer your question: https://stackoverflow.com/questions/36297546/in-active-directory-what-is-a-resource?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – wh-dev Jun 08 '18 at 13:12
2

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

Community
  • 1
  • 1
Taylor Maxwell
  • 140
  • 1
  • 9