2

I am using Adal (Active Directory Authentication Library) Version 3.13.x. I am doing something like below to fetch the Access token

AuthResult = await AuthContext.AcquireTokenAsync(relyingUrl, ServiceConstants.CLIENTID, ServiceConstants.RETURNURI, param);

I need to pass the UserCredentials along as well, but right now I can only pass one parameter to the UserCredential() unlike in the Versions 2.x.x of Adal.

I also tried using UserPasswordCredential as suggested in this solution , but since I want to Fetch the token from a Mobile app, I only have access to .net Core and hence can not use UserPasswordCredential

I want to pass the username and password together while acquiring the token. Is there any way i can achieve this?

Any help would be appreciated.

EDIT

After trying out the solution from Fei Xue - MSFT, i get the following 503 error.

enter image description here

Community
  • 1
  • 1
Pooja Gaonkar
  • 1,546
  • 17
  • 27

1 Answers1

1

I want to pass the username and password together while acquiring the token. Is there any way i can achieve this?

In this scenario, we can perform the REST request directly instead of using the client library. Here is a sample for your reference:

Post:https://login.microsoftonline.com/{tenant}/oauth2/token

resource={resource}&client_id={clientId}&grant_type=password&username={userName}&password={password}

Update

string authority = "https://login.microsoftonline.com/{tenantid}";
string resrouce = "https://graph.windows.net";

string clientId = "";
string userName = "";
string password = "";
UserPasswordCredential userPasswordCredential = new UserPasswordCredential(userName, password);
AuthenticationContext authContext = new AuthenticationContext(authority);
var token = authContext.AcquireTokenAsync(resrouce, clientId, userPasswordCredential).Result.AccessToken;

Console.WriteLine(token);
Fei Xue
  • 14,369
  • 1
  • 19
  • 27
  • Thanks for replying. any idea how I can do this with the authority being AD FS? – Pooja Gaonkar Feb 09 '17 at 06:20
  • Not familiar with AD FS, however you can try to use the library to acquire the token using this flow and capture the request via Fiddler for the detail. – Fei Xue Feb 09 '17 at 06:35
  • Please check my Edit. I am quite new to this hence the issues. – Pooja Gaonkar Feb 09 '17 at 06:55
  • Was the request successful if you send using the Adal library? – Fei Xue Feb 09 '17 at 07:06
  • I haven't tried using this approach with adal. Can you please show me how? – Pooja Gaonkar Feb 09 '17 at 07:12
  • @PoojaGaonkar I have updated the post to append a code sample to use **Resource Owner Password Credentials** flow. However this C# code sample is for acquiring the token from the Azure AD. Hope it is helpful. – Fei Xue Feb 10 '17 at 06:27
  • Thanks for the answer. But as I mentioned in the Question since I am doing this on a mobile app, I only have access to .net Core. Hence I cannot access UserPasswordCredential – Pooja Gaonkar Feb 10 '17 at 06:32
  • The code sample is used to help us to capture the request which parameters we need to perform. So that we can compose the request ourselves as the original post I suggested. – Fei Xue Feb 10 '17 at 06:38