22

I try to setup authentification for an Angular app using authorization code and Azure AD B2C (oidc-client on client side), but I'm getting these errors from Angular:

enter image description here

After looking in B2C audit logs, I found this error message:

Clients must send a client_secret when redeeming a confidential grant.

Here's my client side configuration:

const settings = {
  stsAuthority: 'https://supportodqqcdev.b2clogin.com/supportodqqcDev.onmicrosoft.com/v2.0/.well-known/openid-configuration?p=B2C_1_SignUpSignInOdqPlatine',
  clientId: '8447df5b-35a0-40a7-944f-5dcce87a2193',
  clientRoot: 'https://localhost:4200',
  scope: 'openid https://supportodqqcDev.onmicrosoft.com/platineclientdev/read',
};
this.userManager = new UserManager({
  authority: settings.stsAuthority,
  client_id: settings.clientId,
  redirect_uri: `${settings.clientRoot}/signin-callback`,
  scope: settings.scope,
  response_type: 'code',
  post_logout_redirect_uri: `${settings.clientRoot}/signout-callback`,
  automaticSilentRenew: true,
  silent_redirect_uri: `${settings.clientRoot}/assets/signin-silent-callback.html`,
});

If I switch the above configuration to use a local IdentityServer instance, everthings works has expected.

Is someone able to point me out where or how I should investigate this?

2
  • 2
    Who will redeem code for you? I guess you are using SPA and trying to integrate Auth Code flow. If you are using SPA then you must use implicit flow. B2C is showing client_secret is must because it needs that to redeem code. replace response_type value with 'token' and see what it is doing Commented Dec 4, 2019 at 1:47
  • 1
    B2C currently supporting PKCE flow for Native Applications but not for Web. Native Applications can redeem auth code by using PKCE flow but not web applications. Commented Dec 5, 2019 at 10:43

5 Answers 5

37
Answer recommended by Microsoft Azure Collective

I had the exact same issue as you and was just able to resolve it.

AD is requesting the client_secret from you, because it isn't configured for PKCE yet. To tell AD that you want to use PKCE for a specific redirect url you need to set its type from 'Web' to 'Spa'. This can be done in the manifest.

Search for the property replyUrlsWithType in the Manifest and look for your .../signin-callback url. Change its type to 'Spa' and you should be good.

eg.:

"replyUrlsWithType": [
    {
        "url": "http://localhost:8080/signin-callback",
        "type": "Spa"
    },
]

The configured url will now disappear from your Authorization page but thats ok -> it's still present in the Manifest. The MS team is working on this new type.

Also make sure you marked your application as a public client.

For more information, see my answer here: Is Active Directory not supporting Authorization Code Flow with PKCE?

15

In the Azure AD B2C App there is now a simpler option to do that. In the Authentication tab where the Web Redirect URIs are you will probably see a message This app has implicit grant settings enabled. If you are using any of these URIs in a SPA with MSAL.js 2.0, you should migrate URIs. When you click that, a new window will let you choose which Redirect URI you want to move to the SPA Redirect URIs instead:

Migrate to the single-page application (SPA)

After that just click Configure and it should work. The Redirect URI will now be located in the SPA section instead of the Web one.

enter image description here

0

Your image shows a CORS error.

I'm not sure if oidc-client works OOTB with B2C. It's more for identityserver.

Have a look at the msal.js sample.

1
  • Thanks I know MSAL. I thought oidc-client was working with any STS? Commented Dec 4, 2019 at 1:08
0

I suspect that your code is fine but ...

The last I heard, Azure AD does not allow cross origin calls to the token endpoint - and therefore does not support the Authorization Code Flow (PKCE) that SPAs should use in 2019.

Unless I'm mistaken this will mean you need to use the (unrecommended) implicit flow when integrating with Azure AD. There have been problems for SPAs for a couple of years now.

Out of interest I wrote a couple of posts on Azure SPA workrounds a couple of years ag - I suspect some of this is still relevant: https://authguidance.com/2017/11/30/azure-active-directory-setup/

9
  • When I get the well known config at https://supportodqqcdev.b2clogin.com/supportodqqcDev.onmicrosoft.com/v2.0/.well-known/openid-configuration?p=B2C_1_SignUpSignInOdqPlatine, code is included in the supported response types. It doesn't meen that Authorization Code Flow is supported? Commented Dec 4, 2019 at 1:11
  • In theory, it should but there are differences. You'll probably find msal.js won't work OOTB with identityserver. Also, I think that MSAL.js uses implicit flow not authorisation code grant.
    – rbrayb
    Commented Dec 4, 2019 at 1:25
  • @MaximeGélinas, you should not use Auth Code flow for SPA apps, out of the box B2C supports Auth Code flow from SPA apps but we should not use that because of high security risk (risk is because, you need to expose client id and secret in client script and any one can easily read that from web). Commented Dec 4, 2019 at 1:52
  • 1
    @Ramakrishna Authz Code Flow is the new way for SPA. Implicit flow is deprecated since summer 2019. See: pluralsight.com/courses/openid-and-oauth2-securing-angular-apps. Commented Dec 4, 2019 at 13:09
  • Just to be clear - Authorization Code Flow (PKCE) is recommended for SPAs and Maxime's code looks fully correct. But Azure AD incorrectly block the request to the token endpoint. I would aim to resolve it by double hopping the request to the token endpoint via a back end API. I will update my sample to do this when I get some time. Commented Dec 5, 2019 at 8:10
0

In few comments, I read it as 'ADB2C currently supporting PKCE flow for Native Applications but not for Web', I know it's bit old discussion thread but I just wanted to inform that currently PKCE is recommended for all applications including web application.

Source

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.