0

I'm really after something like this: https://github.com/Azure-Samples/active-directory-dotnet-native-headless which used to work in version 3.9 but not now.

Specifically: authContext.AcquireTokenAsync(todoListResourceId, clientId, uc).Result;

So is it possible to do the same thing using the REST API?

This is for a functional test so we can't allow the user to login normally. I need to replicate what a user would see after logging in.

1 Answers1

0

Yes. It is possible to acquire the token using the HTTP request directly for the Resource Owner Password Credentials flow.

We can use the Fiddler to capture the request to see the detail parameter required. And here is a code sample to use this flow to get the access token for the native client app:

string resrouce = "https://graph.windows.net";
string clientId = "";
string userName = "";
string password = "";

HttpClient client = new HttpClient();  
string body = String.Format("resource={0}&client_id={1}&grant_type=password&username={2}&password={3}", Uri.EscapeDataString(resrouce), clientId, Uri.EscapeDataString(userName), Uri.EscapeDataString(password));
StringContent sc = new StringContent(body,Encoding.UTF8, "application/x-www-form-urlencoded");
var resoult= client.PostAsync("https://login.microsoftonline.com/xxxx.onmicrosoft.com/oauth2/token", sc).Result.Content.ReadAsStringAsync().Result;

And if you are working with web app, we need to append the client_secret parameter in the body.

Fei Xue
  • 14,369
  • 1
  • 19
  • 27