You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Feature Request] During auth code redemption, send code_verifier to /token endpoint as part of auth code + pkceΒ #470
Background
For details, see the spec for auth code + pkce for public clients.
Auth code + pkce was originally implemented for public clients as they are susceptible to having the auth code intercepted. However, more and more confidential clients are wanting to implement pkce:
Part of this work is adding a client secret for the web app, in the templates, now as the secret will be used to verify the client.
Next Steps
MSAL .NET needs to support auth code + pkce for confidential clients first, so that we can use it during auth code redemption. See issue here.
ASP NET starts the first leg of the auth code flow, and includes code_challenge and code_challenge_method to the /authorize endpoint.
Then, the second part, MS Identity Web, using MSAL, will need to send the code + code_verifier (which we will get in the AuthenticationProperties) so that the server can verify the original code_challenge.
We need to complete the work in MSAL .NET first.
We can use the MSAL Java implementation for this.
What I'd like to see for this:
A new override of AcquireTokenByAuthorizationCode that takes the code_verifier. The changes to Id Web should be minimal, 1-2 lines. Just getting the code_verifier from the event context on the AuthenticationProperties in ASP.NET and giving to the AcquireTokenByAuthorizationCode method.
Like this for example:
publicasyncTaskAddAccountToCacheFromAuthorizationCodeAsync(AuthorizationCodeReceivedContextcontext,IEnumerable<string>scopes){if(context==null){thrownewArgumentNullException(nameof(context));}if(scopes==null){thrownewArgumentNullException(nameof(scopes));}try{_application=awaitGetOrBuildConfidentialClientApplicationAsync().ConfigureAwait(false);context.TokenEndpointRequest.Parameters.TryGetValue(OAuthConstants.CodeVerifierKey,outstring?codeVerifier);varbuilder=_application.AcquireTokenByAuthorizationCode(scopes.Except(_scopesRequestedByMsal),context.ProtocolMessage.Code).WithSendX5C(_microsoftIdentityOptions.SendX5C).WithPkceCodeVerifier(codeVerifier);....//more here
Jean-Marc Prieur (@jmprieur)jennyf19 for PKCE flows in B2C, you need a "Single Page application" platform, although the name is a bit confusing here. As stated in the initial post of this Issue, PKCE can/should now also be used by confidential web applications. So I assume selecting "Single Page application" is what we need to get Auth code + PKCE in B2C play nice with the new bits introduced with latest version (1.9.2) of this library?
Ted van der Veen (@tedvanderveen), I think that this is because you don't need to do anything in the B2C app registration, in order to enable PKCE in a B2C web app.
So, no. don't add "Single Page application". It would only work with blazorwasm applications or MSAL.js applications.
Jean-Marc Prieur (@jmprieur) is correct, you shouldn't have to do anything as app developer using Microsoft Identity Web to enable PCKE, it's on by default and your app should just work as it did before, with no code or registration changes.
jennyf19Jean-Marc Prieur (@jmprieur) FYI: I got it working finally with the B2C Single Page application.
Trick was to add an additional scope (see last line) for the client_id:
Ted van der Veen (@tedvanderveen) , that is what I was doing because I am receiving an error, which I believe is telling me switch from web to spa on the app registration. I also have ot change the call backs. What I would like to ask you is why you need the
I mean, it seems the UsePkce is on by default, so I wonder why not everything is wired on by default. When I put the two options above, I get the code_challenge in the url, so it is good news (then it crashes because I need to configure to spa in azure, but that is ok). If I remove the two options, I do not see the code challenge. Any thought?
for your screenshot, I think it should work if you leave the public client switch off, correct? I have another app which is blazor wasm and it uses b2c with pkce, and it is basically your screenshot but with public/native switched off, and this makes sense to me.
Ponant, Ted van der Veen (@tedvanderveen) : you don't need to do anything to activate PKCE. This is done on the web app (code) side. the app registration does not change at all. In particular the redirect URI for a webapp, should be Web, not SPA, unless you're building a blazorwasm application on .NET 5.0 or later
I can confirm anew. I tested several options (even knowingly that it won't work).
In the portal, with a web option:
If I disable implicit grant (Access and idtokens checkboxes) I get the error (before login flow). And Url has no code_challenge
AADB2C90057: The provided application is not configured to allow the 'OAuth' Implicit flow.
If I enable only ID tokens (used for implicit and hybrid flows), the same error.
If I add to the startup.cs options.ResponseType = OpenIdConnectResponseType.Code; options.Scope.Add(options.ClientId);
Then I see a code_challenge in the url
Then I can go through the login flow but then it throws 'AADB2C90079: Clients must send a client_secret when redeeming a confidential grant.
If I remove the code above, which is essentially the base template, then the app works but no code_challenge
Jean-Marc Prieur (@jmprieur) , I checked out further, and the only way to make it work from the base template (updated nuget) is to do on the portal a spa registration, so Ted van der Veen (@tedvanderveen) is right. So I do not understand how you get it to work with the web option in azure?
The portal even hapily displays "Your Redirect URI is eligible for the Authorization Code Flow with PKCE."
Here is the output manifest
Edit: Furthermore, the options.ResponseType = OpenIdConnectResponseType.Code; options.Scope.Add(options.ClientId); need to be added in startup. SO I believe we disagree.
Activity
jmprieur commentedon Mar 17, 2021
See also https://identitydivision.visualstudio.com/Engineering/_workitems/edit/1323064
jennyf19 commentedon Mar 23, 2021
What I'd like to see for this:
A new override of AcquireTokenByAuthorizationCode that takes the code_verifier. The changes to Id Web should be minimal, 1-2 lines. Just getting the code_verifier from the event context on the AuthenticationProperties in ASP.NET and giving to the AcquireTokenByAuthorizationCode method.
Like this for example:
jmprieur commentedon Apr 22, 2021
Bloked until MSAL.NET 4.30 releases
5 remaining items
tedvanderveen commentedon May 6, 2021
Jean-Marc Prieur (@jmprieur) jennyf19 for PKCE flows in B2C, you need a "Single Page application" platform, although the name is a bit confusing here. As stated in the initial post of this Issue, PKCE can/should now also be used by confidential web applications. So I assume selecting "Single Page application" is what we need to get Auth code + PKCE in B2C play nice with the new bits introduced with latest version (1.9.2) of this library?
jmprieur commentedon May 6, 2021
Ted van der Veen (@tedvanderveen), I think that this is because you don't need to do anything in the B2C app registration, in order to enable PKCE in a B2C web app.
So, no. don't add "Single Page application". It would only work with blazorwasm applications or MSAL.js applications.
jennyf19 commentedon May 6, 2021
Jean-Marc Prieur (@jmprieur) is correct, you shouldn't have to do anything as app developer using Microsoft Identity Web to enable PCKE, it's on by default and your app should just work as it did before, with no code or registration changes.
tedvanderveen commentedon May 6, 2021
jennyf19 Jean-Marc Prieur (@jmprieur) FYI: I got it working finally with the B2C Single Page application.
Trick was to add an additional scope (see last line) for the client_id:
Ponant commentedon May 11, 2021
Ted van der Veen (@tedvanderveen) , I have the same question, so you did register as SPA + the configuration above, and you can get pkce?
tedvanderveen commentedon May 11, 2021
Ponant yes it just works!
Ponant commentedon May 11, 2021
Razor pages? It does not work locally, but I just implemented so I will check further.
tedvanderveen commentedon May 11, 2021
Did you set B2C up with the settings like what I posted here in the screenshot?
Ponant commentedon May 11, 2021
Ted van der Veen (@tedvanderveen) , that is what I was doing because I am receiving an error, which I believe is telling me switch from web to spa on the app registration. I also have ot change the call backs. What I would like to ask you is why you need the
Ponant commentedon May 11, 2021
I mean, it seems the UsePkce is on by default, so I wonder why not everything is wired on by default. When I put the two options above, I get the code_challenge in the url, so it is good news (then it crashes because I need to configure to spa in azure, but that is ok). If I remove the two options, I do not see the code challenge. Any thought?
Ponant commentedon May 11, 2021
for your screenshot, I think it should work if you leave the public client switch off, correct? I have another app which is blazor wasm and it uses b2c with pkce, and it is basically your screenshot but with public/native switched off, and this makes sense to me.
jmprieur commentedon May 11, 2021
Ponant, Ted van der Veen (@tedvanderveen) : you don't need to do anything to activate PKCE. This is done on the web app (code) side. the app registration does not change at all. In particular the redirect URI for a webapp, should be Web, not SPA, unless you're building a blazorwasm application on .NET 5.0 or later
Ponant commentedon May 11, 2021
If I do this (with razor pages .net 5 b2c, upgraded to latest nuget 1.9.2), then I do not get the code_challenge sent out
Ponant commentedon May 11, 2021
I can confirm anew. I tested several options (even knowingly that it won't work).
In the portal, with a web option:
If I disable implicit grant (Access and idtokens checkboxes) I get the error (before login flow). And Url has no code_challenge
AADB2C90057: The provided application is not configured to allow the 'OAuth' Implicit flow.
If I enable only ID tokens (used for implicit and hybrid flows), the same error.
If I add to the startup.cs
options.ResponseType = OpenIdConnectResponseType.Code; options.Scope.Add(options.ClientId);Then I see a code_challenge in the url
Then I can go through the login flow but then it throws
'AADB2C90079: Clients must send a client_secret when redeeming a confidential grant.
If I remove the code above, which is essentially the base template, then the app works but no code_challenge
Ponant commentedon May 11, 2021
Jean-Marc Prieur (@jmprieur) , I checked out further, and the only way to make it work from the base template (updated nuget) is to do on the portal a spa registration, so Ted van der Veen (@tedvanderveen) is right. So I do not understand how you get it to work with the web option in azure?
The portal even hapily displays "Your Redirect URI is eligible for the Authorization Code Flow with PKCE."
Here is the output manifest
Edit: Furthermore, the
options.ResponseType = OpenIdConnectResponseType.Code; options.Scope.Add(options.ClientId);need to be added in startup. SO I believe we disagree.