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#?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With