App Registrations are required to set up security on Web API using On-Behalf-Of flow. The On-Behalf-Of flow requires setting up two app regsitrations - one for server side and another one for client side; and linking them. There are two ways - using script or manual set up to create these app registrations. Both of these approaches cover creation of server and client app registration and linking them to work in On-Behalf-Of flow.
You need to clone the repository for this step: https://github.com/Azure-Samples/communication-services-authentication-hero-csharp
# HTTPS
git clone https://github.com/Azure-Samples/communication-services-authentication-hero-csharp.git
# SSH
git clone [email protected]:Azure-Samples/communication-services-authentication-hero-csharp.git
Once the script is run in local environment following the instructions, the AzureActiveDirectory
fields are updated in src/appsettings.json from service app registration and msalConfig.auth
fields are updated in MinimalClient/src/authConfig.js from client app registration in the cloned repository.
Follow below instructions on how to register your server application with Azure Active Directory here.
-
When registering your server app, use the following information:
- give your application a meaningful name as this will be the displayed name of your app, for example
auther-server-sample-webApi
. - select the Accounts in this organizational directory only ( only - Single tenant) option for who can use or access this application.
Note: clicking on the Register button will open your application page once the registration is sucessful.
- give your application a meaningful name as this will be the displayed name of your app, for example
-
On the app's registration screen (auther-server-sample-webApi page):
-
Navigate to and click on Certificates & secrets menu item on the left to open the page where you can generate secrets and upload certificates.
- In the Client secrets section, click on New client secret to create a new one.
- Type a key description (for instance
app secret
). - Select one of the available key durations as per your security posture.
- The generated key value will be displayed when you click on the Add button. Copy the generated value for use in the steps later.
Note: You'll need this key later on in your code's configuration files. This key value will not be displayed again, and is not retrievable by any other means, so make sure to note it from the Azure portal before navigating to any other screen or page.
-
Navigate to and click on API permissions menu item on the left to open the page where access to the APIs needed by your application will be defined.
- Click on Add a permission.
- Select Microsoft Graph.
- Select Delegated permissions for the type of permissions required by your app.
- On the permission list, scroll to User group and expand it, then check User.Read and User.ReadWrite.All.
- Now click on the Add permissions button at the bottom to save your permissions.
- Once the permissions are added, click on Grant admin consent for the Microsoft Graph API call.
Note: The 'Grant admin consent' step can only be performed by your Azure Active Directory Admin.
-
Navigate to and click on the Expose an API menu item on the left to open the page where you can declare the parameters to expose this app as an API from which client applications can obtain the access tokens.
-
The first thing that you need to do is to declare the unique resource URI that the clients will be using to obtain access tokens for this API. To declare a resource URI, follow the steps below:
- Select on
Set
next to the Application ID URI to generate an URI that is unique for this app. - For this sample, accept the proposed Application ID URI (
api://{clientId}
) by clicking on the Save button.
- Select on
-
All APIs have to publish a minimum of one scope for the client to successfully obtain an access token. To publish a scope, click on the Add a scope button. This will open the Add a scope pane where you can defined your scope's values as indicated below:
- For Scope name, use
access_as_user
. - For Who can consent? option, select Admins and users.
- For Admin consent display name, type
Access Microsoft Graph API
. - For Admin consent description, type
Allows the app to access Microsoft Graph API as the signed-in user.
- For User consent display name, type
Access Microsoft Graph API
. - For User consent description, type
Allow the application to access Microsoft Graph API on your behalf.
- Keep the State as Enabled.
- Click on the Add scope button at the bottom to save this scope.
- For Scope name, use
-
-
Navigate to and click on Manifest menu item on the left.
- In the editor, set
accessTokenAcceptedVersion
property to 2. - Click on Save on the top bar.
- In the editor, set
-
Note - This client app registration will be used to manually generate the Azure Active Directory Token required to call Azure Active Directory protected Web API as there is no client application in the sample.
Follow instructions on how to register your client application with Azure Active Directory here.
-
When registering your client app, use the following information:
- give your application a meaningful name as this will be the displayed name of your app, for example
auther-server-sample-webClient
. - select the Accounts in this organizational directory only ( only - Single tenant) option for who can use or access this application.
- set the Redirect URI (optional) with Single-page Application (SPA) as platform and
http://localhost:3000/
as URI. In case of manual generation of Azure Active Directory token for testing Auth Sample Apis, select Web as platform instead of SPA.
Note: clicking on the Register button will open your application page once the registration is sucessful.
- give your application a meaningful name as this will be the displayed name of your app, for example
-
On the app's registration screen (auther-server-sample-webClient page):
-
Navigate to and click on Certificates & secrets menu item on the left to open the page where you can generate secrets and upload certificates.
- In the Client secrets section, click on New client secret to create a new one.
- Type a key description (for instance
app secret
). - Select one of the available key durations as per your security posture.
- The generated key value will be displayed when you click on the Add button. Copy the generated value for use in the steps later.
Note: You'll need this key later on in your code's configuration files. This key value will not be displayed again, and is not retrievable by any other means, so make sure to note it from the Azure portal before navigating to any other screen or page.
-
Navigate to and click on API permissions menu item on the left to open the page where access to the APIs needed by your application will be defined.
- Click on Add a permission.
- Ensure that the My APIs tab is selected.
- In the list of APIs, select the API
auther-server-sample-webApi
.
Attention: If the exposed API of created server app registration does not appear in the list, please refresh your page. The chances are the API list is not updated properly since you just exposed a new Api on the server app registration.
- In the Delegated permissions section, select
access_as_user
in the list. Use the search box if necessary. - Now click on the Add permissions button at the bottom to save your permissions.
-