Protect web API with Ad b2c authorization
Protect web API with Ad b2c authorization
Azure ad b2c supports OIDC for identification and Oauth for authentication. Once a user is signed in to
your app, the app then requests an access token from Ad b2c. the access token is a JWT signed by AD
b2c and is valid for certain period of time. It contains required to access the API. Client App uses this
token as bearer authorization when calling your protected API. The API configured to be protected
with AD b2c, ensures validity and integrity of the token and that it contains requiered permission to
access that API and then allows the API to be executed.
In order to get this working, we first need to set up the ad b2c tenant with the app registration,
configure API permission for the app registration and then configure the client and API app code to
consume this configuration. Sample code for both client and API app are provided in this post.
In order to configure the scopes, first create a URI for your API app. This URI is only a unique
indentification for you API app with AD b2c. It is not related to the actual URI or URL of your API app
runing somewhere in the cloud or you local system.
As mentioned earlier, scope is essentially a permission to access the APIs. This permission is granted
to the client app. When the client app accesses an API, it can do so on behalf of the user.
This called delegated authorization. Effectively, the client app is acting as the user. In other
words user delegates permission to the client app to access API on their behalf.
Users or admins of the tenant need to provide their consent for a client app to access the API
resources on their behalf. The admin consent display name and admin consent description
are used to display information to the admisn so they can grant access tenant wide on
behalf of all users.
ADD API PERMISSIONS FOR CLIENT APP
The permission that we are adding for our app to access backend api is called as a delegated
permission. The app uses this permission to access backend APIs on behalf of the user
which neans that the user effectively delegates the permission to access the resource to
the app. For delegated permissions like this, we need to provide admin consent before the
app can use that permission.
- Protecting your APIs from unauthorized access is important. Azure ad b2c helps you to validate the
user or app accessing the API and allows access to the API only with authorized token
obtained from AD b2c.
Steps:
1. Setting up a tenant in azure ad b2c ( manage consumer identity)
2. Create user flows in azure ad b2c
3. Use Azure ad b2c in react spab2c
….
………………………………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………………………………
……………………………
Azure b2c supports openid connect for identification and OAuth for authorization.
Once a user is signed in to your app, the app then request an access token form Azure ad b2c. the
access token is a json web token(jwt) signed by ad b2c and is valid for a certain period of time. It
contains required scopes and other claims or attributes. Scopes are akin(relacionado,类似的) to
permissions requiered to access the API. Client app uses this token as Bearer authorization when
calling your protected API. The API configured to be protected with AD b2c, ensures validity and
integrity of the token and that it contains required permissions to access that API and then allows the
API to be executed.