Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Login to Azure Active Directory programmatically

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#?

like image 348
Cuthbert Avatar asked Oct 30 '22 05:10

Cuthbert


1 Answers

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.

like image 116
Gaurav Mantri Avatar answered Nov 15 '22 04:11

Gaurav Mantri