Skip to content
This repository was archived by the owner on Jan 23, 2020. It is now read-only.

Conversation

@jmprieur
Copy link
Contributor

Updating the sample to ASP.NET Core 2.0

It's probably better to skip commit bdd265d which brings a lot of churn by copying code from already upgraded samples (see below)
The following commits are simpler to understand

Code for the service

The code for the service is exactly the same as in the active-directory-dotnet-native-aspnetcore same. Please refer to the readme of that sample to understand how to it was built

Code for ASP.NET Web App

The code for the ASP.NET Web App is based on the code of the active-directory-dotnet-webapp-openidconnect-aspnetcore sample. Please read the "About The code" section of that sample first.

Then, based on that code, the following modifications were applied:

  • Update of the AzureAdOptions class to add a property to compute the Authority from the instance and the tenantID, and adding two other configuration options for ClientSecret, the resourceId of TodoListService (its clientId) and the base address for this service.
  • Added a TodoListItem in models to deserialize the Json sent by the TodoListService
  • Added a NaiveSessionCache class in a new Utils folder which serves as a token cache which livetime is the duration of the session. Updated the Startup.cs file accordingly to add sessions.
  • Added a TodoListController and a Todo view, as well as a "Todo List" entry in the toolbar of the Web API. This is where most of the interesting code is
  • Updated the SignOut() method of the AccountController to clear the cache for the user when s/he signs-out.
  • Updated AzureAdAuthenticationBuilderExtensions.cs to request an authorization code, and redeem it, getting an access token to the Azure AD graph (https://graph.windows.com), so that the token cache contains a token for the user. This token will be used by the TodoController to request another token for the TodoListService

Taking:
-  the TodoListWebApp from active-directory-dotnet-webapp-openidconnect-aspnetcore
- the TodoListService from active-directory-dotnet-native-aspnetcore
The way I proceeded is starting from:
- the TodoListService of the following sample https://github.com/Azure-Samples/active-directory-dotnet-native-aspnetcore (the code for the TodoListService is identifical)
- the open id connect WebApp which is the object of the following sample: https://github.com/Azure-Samples/active-directory-dotnet-webapp-openidconnect-aspnetcore sample
This was the object of commit bdd265d
- In the TodoListService and the TodoListWebApp upgrading from AspNetCore 2.0.3 to 2.0.5

Then, from the the OpenIdConnect WebApp for ASP.Net Core, I've enabled calling the TodoListService
- Updating the AzureAdOptions to compute the Authority from the instance and the tenantID, and adding two other configuration options for the resourceId of the TodoListService (its clientId) and the base address for this service.
- Adding a TodoListItem in models
- Adding a NaiveSessionCache class in a new Utils folder
- Adding a TodoListController and a TodoList view, as well as a "Todo List" entry in the toolbar of the Web API
- Update the SignOut() method of the AccountController to clear the cache for the user.
- Updating AzureAdAuthenticationBuilderExtensions to request an authorization code, and redeem it so that the token cache contains a token for the user, so that it can be used by the TodoController
@acomsmpbot
Copy link
Contributor

No issues were found in this pull request.

@acomsmpbot
Copy link
Contributor

No issues were found in this pull request.


<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.3" />
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.5" />
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated the NuGet package from ASP.NET Core 2.0.3 to 2.0.5

Copy link
Contributor Author

@jmprieur jmprieur left a comment

Choose a reason for hiding this comment

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

I've annotated the pull request with comments to explain certain interesting aspects.

// Without overriding the response type (which by default is id_token), the OnAuthorizationCodeReceived event is not called.
// but instead OnTokenValidated event is called. Here we request both so that OnTokenValidated is called first which
// ensures that context.Principal has a non-null value when OnAuthorizeationCodeReceived is called
options.ResponseType = "id_token code";
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this bit seems to be a change of behavior in ASP.NET Core 2.0. This also provide an answer to issue #15

/// <summary>
/// Instance of the settings for this Web application (to be used in controllers)
/// </summary>
public static AzureAdOptions Settings { set; get; }
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These settings need to be accessed from several locations, including the controller. Since they won't change during the execution of the Web App, I've chosen to provide them as a static member.


app.UseStaticFiles();

app.UseSession(); // Needs to be app.UseAuthentication() and app.UseMvc() otherwise you will get an exception "Session has not been configured for this application or request."
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The position of UseSession() relatively to UseAuthentication() and UseMvc() is important (it needs to be just before them).

// Coordinates of the TodoListService
"TodoListResourceId": "[Enter the Client Id (Application ID) of the TodoListService, obtained from the Azure portal), e.g. 11111111-1111-1111-11111111111111111]", // ClientId of the TodoList Service
"TodoListBaseAddress": "https://localhost:44351"
},
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The appsettings.json for the ASP.NET application got enriched with:

  • the client secret of the TodoList Web App, which is necessary for the confidential client flow to get a token for the TodoListService Web API
  • the coordinates of the TodoListService (resourceId and base URL)

@Rick-Anderson
Copy link

@jmprieur is README.md the document? I couldn't find another .md file. Are you looking for a technical review, editor review, testing instructions review?

@Rick-Anderson
Copy link

@jmprieur Can you ping the reviewers so we can get this published?

@CamSoper CamSoper mentioned this pull request Feb 1, 2018
@acomsmpbot
Copy link
Contributor

No issues were found in this pull request.

@jmprieur jmprieur merged commit 5fef217 into master Feb 2, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants