6

I have a site (app) registered with Azure Active Directory. I need my web tests to authenticate themselves (with a pre-existing testing user) at the start in order to obtain an auth token for the tests to hit the protected APIs.

What's the best way to accomplish this in C#?

Cuthbert
  • 1,202
  • 1
  • 12
  • 18

1 Answers1

2

You can try doing something like below:

var authContext = new AuthenticationContext("https://login.microsoftonline.com/{tenantid}")
    UserCredential userCredential = new UserCredential(userName, password);
    AuthenticationResult authResult = authContext.AcquireToken("https://graph.windows.net/", clientId, userCredential);

Where userName and password are the user name and password of your test user. authResult has a member called AccessToken that can be passed to the methods you wish to test.

Gaurav Mantri
  • 128,066
  • 12
  • 206
  • 241
  • When I try this from a CLI test app, I get a "The request body must contain the following parameter: 'client_secret or client_assertion" exception. – Cuthbert Apr 11 '16 at 19:07
  • Based on http://stackoverflow.com/questions/26846357/adal-the-request-body-must-contain-the-following-parameter-client-secret, I believe you can only use the approach I mentioned for native apps and not web apps. – Gaurav Mantri Apr 12 '16 at 06:16
  • how about in nodejs – Ujjual Feb 20 '20 at 10:34
  • What is client id? Should be able to log in with just a username and password, just like on the portal. – Triynko Dec 09 '20 at 04:18