Azure Functions and ADB2C integration

I have an Azure Functions application (API), which consists of HTTP-triggered functions.
Also, I have Azure Active Directory B2C tenant and signup/signin policy, which I use for user identities.

I planned following flow for working with API:

  1. User registers/authenticates in a web application using ADB2C + MSAL
  2. User makes requests to my API, passing access_token value from the authentication to Authorization header
  3. Azure automatically processes token, extracts user data, and makes it available in request.user field inside my function.

My API function code:

const { app } = require("@azure/functions");

app.http("HelloWorld", {
  methods: ["GET", "POST"],
  authLevel: "anonymous",
  handler: async (request, context) => {
    context.log(`Http function processed request for url "${request.url}"`);

    // Debug
    context.log(request.user);

    const name = request.query.get("name") || (await request.text()) || "world";
    const user = request.user ? JSON.stringify(user) : null;

    return { body: `Hello, ${name}! User data: ${user}` };
  },
});

I tried to enable auth from Azure Functions -> My App -> Preferences -> Authentication settings.

It created a new App Registration in ADB2C.
However this app registration has following notification:

“You can use this application to authenticate against Microsoft Entra ID, but not Azure AD B2C.”

Also, token acquired from this app registration did not authorize my requests to function.

But there was interesting moment:

I could reach functions app url, which got me through auth flow (which looked like standard MS auth, not my policy), then redirected to page saying Your Functions 4.0 app is up and running.

I’ve tried to perform HTTP request from this page using developer console, and it worked.
I’ve checked the request info in Network tab and discovered that my request was authorized by Cookie AppServiceAuthSession.


I also tried different approach.

I’ve created App Registration manually, attached it to function app, set jwt.ms as a redirect path, acquired token this way, but when trying to authenticate my request with this token, it didn’t work.

I also tried accessing Function App url, it went me through auth process, but after entering creds it redirected my to https://<my-app-url>/.auth/login/aad/callback with message:

You do not have permission to view this directory or page.


Third approach was a hacky: I tried to use automatically created app registration, but with issuer URL from App Registration I’ve created manually. I got following error after trying to authenticate:
The provided application with ID 'APP_ID' is not valid against this service. Please use an application created via the B2C portal and try again.

It makes me think that this functionality is incompatible with ADB2C and I need to write a middleware to do the job. But a confirmation from experienced Azure users or team members would be highly appreciated.


While trying to make it work I used following articles as a reference:

Elio Struyf: Securing your Azure Functions with an existing Azure AD app

Arjen van Benten: How to secure Azure Functions with Azure B2C

And Azure Docs.

I tried both headers: Authorization: Bearer %access_token% and Authorization: %access_token%.


Questions:

  1. Is my logic correct about how it should work?

  2. Is it possible to authenticate requests with Authorization token, or it can be made by using cookies only?

  3. Should I use Microsoft Entra ID or Microsoft Entra External ID instead of ADB2C?

To secure Azure Function using Azure AD B2C check the below:

Create an Azure AD B2C application and expose an API:

Grant API permissions for the exposed scope:

I created an HTTP trigger:

  • Make sure to add redirect URL as https://YourfunctionAppName.azurewebsites.net/.auth/login/aad/callback

Added Identity provider as Microsoft and passed the values of Azure AD B2C application:

The issuer must be in the format https://b2c.b2clogin.com/b2c.onmicrosoft.com/v2.0/.well-known/openid-configuration?p=B2C_1_susi

Using Postman I generated access token and able to call the HTTP trigger successfully:

GET https://rukkfa.azurewebsites.net/api/HttpTrigger1

Grant type: Authorization code 

Callback URL: https://FunctionAppName.azurewebsites.net/.auth/login/aad/callback
Auth URL:  https://b2c.b2clogin.com/b2c.onmicrosoft.com/B2C_1_testruk/oauth2/v2.0/authorize
Token URL : https://b2c.b2clogin.com/b2c.onmicrosoft.com/B2C_1_testruk/oauth2/v2.0/token
Client ID : ClientID
Client Secret : ClientSecret
Scope: https://b2c.onmicrosoft.com/ClientID/API.Call

Decoded access token:

Recognized by Microsoft Azure Collective

20

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật