Scenario: Native App calling Web API
Applies To: AD FS 2019 and later
Learn how to build a native app signing-in users authenticated by AD FS 2019 and acquiring tokens using MSAL library to call web APIs.
In this flow you add authentication to your Native App (public client), which can therefore sign in users and calls a Web API. To call a Web API from a Native App that signs in users, you can use MSAL's AcquireTokenInteractive token acquisition method. To enable this interaction, MSAL leverages a web browser.
To better understand how to configure a Native App in ADFS to acquire access token interactively, let's use a sample available here and walkthrough the app registration and code configuration steps.
- GitHub client tools
- AD FS 2019 or later configured and running
- Visual Studio 2013 or later
App Registration in AD FS
This section shows how to register the Native App as a public client and Web API as a Relying Party (RP) in AD FS
In AD FS Management, right-click on Application Groups and select Add Application Group.
On the Application Group Wizard, for the Name enter NativeAppToWebApi and under Client-Server applications select the Native application accessing a Web API template. Click Next.
Copy the Client Identifier value. It will be used later as the value for ClientId in the application's App.config file. Enter the following for Redirect URI: https://ToDoListClient. Click Add. Click Next.
On the Configure Web API screen, enter the Identifier: https://localhost:44321/. Click Add. Click Next. This value will be used later in the application's App.config and Web.config files.
On the Apply Access Control Policy screen, select Permit everyone and click Next.
On the Configure Application Permissions screen, make sure openid is selected and click Next.
On the Summary screen, click Next.
On the Complete screen, click Close.
In AD FS Management, click on Application Groups and select NativeAppToWebApi application group. Right-click and select Properties.
On NativeAppToWebApi properties screen, select NativeAppToWebApi – Web API under Web API and click Edit…
On NativeAppToWebApi – Web API Properties screen, select Issuance Transform Rules tab and click Add Rule…
On Add Transform Claim Rule Wizard, select Transform an Incoming Claim from the Claim rule template: dropdown and click Next.
Enter NameID in Claim rule name: field. Select Name for Incoming claim type:, Name ID for Outgoing claim type: and Common Name for Outgoing name ID format:. click Finish.
Click OK on NativeAppToWebApi – Web API Properties screen and then NativeAppToWebApi Properties screen.
This section shows how to configure a Native App to sign-in user and retrieve token to call the Web API
Download the sample from here
Open the sample using Visual Studio
Open the App.config file. Modify the following:
ida:Authority - enter h
ttps://[your AD FS hostname]/adfs
ida:ClientId - enter the Client Identifier value from #3 in App Registration in AD FS section above.
ida:RedirectUri - enter the Redirect URI value from #3 in App Registration in AD FS section above.
todo:TodoListResourceId – enter the Identifier value from #4 in App Registration in AD FS section above
ida: todo:TodoListBaseAddress - enter the Identifier value from #4 in App Registration in AD FS section above.
Open the Web.config file. Modify the following:
ida:Audience - enter the Identifier value from #4 in App Registration in AD FS section above
ida: AdfsMetadataEndpoint - enter
https://[your AD FS hostname]/federationmetadata/2007-06/federationmetadata.xml
Test the sample
This section shows how to test the sample configured above.
Once the code changes are made rebuild the solution
On Visual Studio, right click on solution and select Set StartUp Projects…
On the Properties pages make sure Action is set to Start for each of the Projects
At the top of Visual Studio, click the green arrow.
On the Native App's Main screen, click on Sign In.
If you don't see the native app screen, search and remove
*msalcache.bin files from the folder where project repo is saved on your system.
You will be re-directed to the AD FS sign-in page. Go ahead and sign in.
Once signed-in, enter text Build Native App to Web Api in the Create a To Do item. Click Add item. This will call the To Do List Service (Web API) and add the item in the cache.