Skip to content

Jmprieur/sync from web api tutorial #186

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 20, 2019
Merged

Conversation

jmprieur
Copy link
Contributor

Purpose

  • ...

Does this introduce a breaking change?

[ ] Yes
[ ] No

Pull Request Type

What kind of change does this Pull Request introduce?

[ ] Bugfix
[ ] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Documentation content changes
[ ] Other... Please describe:

How to Test

  • Get the code
git clone [repo-address]
cd [repo-name]
git checkout [branch-name]
npm install
  • Test the code

What to Check

Verify that the following are valid

  • ...

Other Information

in TokenAcquisition.cs from the code which is currently
in the ASP.NET Core Web API tutorial
/// This method appends in the HttpResponse being sent back to the client, a 403 (forbidden) status code and populates the 'WWW-Authenticate' header with additional information.
/// The client, when it receives the 403 code with this header, can use the additional information provided in the header to trigger an interaction with the user where the user
/// can then consent to additional scopes.
/// Used in Web APIs (which therefore cannot have an interaction with the user).
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better comments (from Jenny)

/// <param name="scopes">The additional scopes that the user needs to consent to</param>
/// <param name="msalServiceException"><see cref="MsalUiRequiredException"/> The MsalUiRequiredException that is examined to see if there is case for this response.</param>

/// <param name="scopes">Scopes to consent to</param>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better comments (from Jenny)

/// <param name="msalServiceException"><see cref="MsalUiRequiredException"/> The MsalUiRequiredException that is examined to see if there is case for this response.</param>

/// <param name="scopes">Scopes to consent to</param>
/// <param name="msalServiceException"><see cref="MsalUiRequiredException"/> triggering the challenge</param>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better comments (from Jenny)

public void ReplyForbiddenWithWwwAuthenticateHeader(IEnumerable<string> scopes, MsalUiRequiredException msalServiceException)
{
// A user interaction is required, but we are in a Web API, and therefore, we need to report back to the client through an wwww-Authenticate header https://tools.ietf.org/html/rfc6750#section-3.1
string proposedAction = "consent";
if (msalServiceException.ErrorCode == MsalError.InvalidGrantError)
{
if (AcceptedTokenVersionIsNotTheSameAsTokenVersion(msalServiceException))
if (AcceptedTokenVersionMismatch(msalServiceException))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better method name (from Jenny)

{
throw msalServiceException;
}
}

string consentUrl = $"{application.Authority}/oauth2/v2.0/authorize?client_id={_azureAdOptions.ClientId}"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consent URL to provision the Web API in multi-tenant / MSA

@@ -38,20 +38,18 @@ public static class WebApiServiceCollectionExtensions
this IServiceCollection services,
IConfiguration configuration,
X509Certificate2 tokenDecryptionCertificate = null,
string configSectionName = "AzureAD",
string configSectionName = "AzureAd",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixing the name of the section (like Kalyan did for the Web App)

bool subscribeToJwtBearerMiddlewareDiagnosticsEvents = false)
{
services.AddAuthentication(AzureADDefaults.JwtBearerAuthenticationScheme)
.AddAzureADBearer(options => configuration.Bind(configSectionName, options));
services.Configure<AzureADOptions>(options => configuration.Bind(configSectionName, options));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixing the binding

@jmprieur
Copy link
Contributor Author

@TiagoBrenck @kalyankrishna1
ping.

@jmprieur jmprieur requested a review from henrik-me September 19, 2019 06:33
Copy link
Contributor

@TiagoBrenck TiagoBrenck left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

I learned something important from this PR: services.Configure<AzureADOptions>(options => configuration.Bind(configSectionName, options)); needs to be after the AddAuthentication() AddAzureADBearer().

@jmprieur jmprieur merged commit bc564d6 into master Sep 20, 2019
@jmprieur jmprieur deleted the jmprieur/syncFromWebApiTutorial branch September 26, 2019 16:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants