Skip to content

Commit 1931345

Browse files
authored
Merge pull request #60 from Azure-Samples/kkrishna/updates2019
Minor fixes after testing
2 parents 1d3237e + 7b702a9 commit 1931345

File tree

5 files changed

+31
-23
lines changed

5 files changed

+31
-23
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,9 @@
2020
/3. WebApp calls several APIS (incremental consent and CA)/.vs
2121
/3. WebApp calls several APIS (incremental consent and CA)/bin
2222
/3. WebApp calls several APIS (incremental consent and CA)/obj
23+
/1. WebApp signs-in users with Microsoft Identity (OIDC)/1.3. with work and school or personal accounts/.vs/WebApp-OpenIDConnect-DotNet/v15
24+
/1. WebApp signs-in users with Microsoft Identity (OIDC)/1.3. with work and school or personal accounts/obj/Debug/netcoreapp2.2
25+
/1. WebApp signs-in users with Microsoft Identity (OIDC)/1.3. with work and school or personal accounts/obj
26+
/2. WebApp calls Microsoft Graph on behalf of signed-in user/2.2/.vs/WebApp-OpenIDConnect-DotNet/v15/Server/sqlite3
27+
/2. WebApp calls Microsoft Graph on behalf of signed-in user/2.2/WebApp-OpenIDConnect-DotNet-Cache-V2/obj
28+
/2. WebApp calls Microsoft Graph on behalf of signed-in user/2.1. using authorization code flow/AppCreationScripts/Steps.md

2. WebApp calls Microsoft Graph on behalf of signed-in user/2.1. using authorization code flow/AppCreationScripts/Cleanup.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ This function removes the Azure AD applications for the sample. These applicatio
4949
# Removes the applications
5050
Write-Host "Cleaning-up applications from tenant '$tenantName'"
5151

52-
Write-Host "Removing 'webApp' (WebApp) if needed"
53-
$app=Get-AzureADApplication -Filter "DisplayName eq 'WebApp'"
52+
Write-Host "Removing 'webApp' (WebApp-OpenIDConnect-DotNet-code-v2) if needed"
53+
$app=Get-AzureADApplication -Filter "DisplayName eq 'WebApp-OpenIDConnect-DotNet-code-v2'"
5454

5555
if ($app)
5656
{

2. WebApp calls Microsoft Graph on behalf of signed-in user/2.1. using authorization code flow/AppCreationScripts/Configure.ps1

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -178,17 +178,17 @@ Function ConfigureApplications
178178
$user = Get-AzureADUser -ObjectId $creds.Account.Id
179179

180180
# Create the webApp AAD application
181-
Write-Host "Creating the AAD application (WebApp)"
181+
Write-Host "Creating the AAD application (WebApp-OpenIDConnect-DotNet-code-v2)"
182182
# Get a 2 years application key for the webApp Application
183183
$pw = ComputePassword
184184
$fromDate = [DateTime]::Now;
185185
$key = CreateAppKey -fromDate $fromDate -durationInYears 2 -pw $pw
186186
$webAppAppKey = $pw
187-
$webAppAadApplication = New-AzureADApplication -DisplayName "WebApp" `
187+
$webAppAadApplication = New-AzureADApplication -DisplayName "WebApp-OpenIDConnect-DotNet-code-v2" `
188188
-HomePage "https://localhost:44321/" `
189189
-LogoutUrl "https://localhost:44321/signout-oidc" `
190190
-ReplyUrls "https://localhost:44321/", "https://localhost:44321/signin-oidc" `
191-
-IdentifierUris "https://$tenantName/WebApp" `
191+
-IdentifierUris "https://$tenantName/WebApp-OpenIDConnect-DotNet-code-v2" `
192192
-AvailableToOtherTenants $True `
193193
-PasswordCredentials $key `
194194
-Oauth2AllowImplicitFlow $true `
@@ -205,19 +205,19 @@ Function ConfigureApplications
205205
Write-Host "'$($user.UserPrincipalName)' added as an application owner to app '$($webAppServicePrincipal.DisplayName)'"
206206
}
207207

208-
Write-Host "Done creating the webApp application (WebApp)"
208+
Write-Host "Done creating the webApp application (WebApp-OpenIDConnect-DotNet-code-v2)"
209209

210210
# URL of the AAD application in the Azure portal
211211
# Future? $webAppPortalUrl = "https://portal.azure.com/#@"+$tenantName+"/blade/Microsoft_AAD_RegisteredApps/ApplicationMenuBlade/Overview/appId/"+$webAppAadApplication.AppId+"/objectId/"+$webAppAadApplication.ObjectId+"/isMSAApp/"
212212
$webAppPortalUrl = "https://portal.azure.com/#blade/Microsoft_AAD_RegisteredApps/ApplicationMenuBlade/CallAnAPI/appId/"+$webAppAadApplication.AppId+"/objectId/"+$webAppAadApplication.ObjectId+"/isMSAApp/"
213-
Add-Content -Value "<tr><td>webApp</td><td>$currentAppId</td><td><a href='$webAppPortalUrl'>WebApp</a></td></tr>" -Path createdApps.html
213+
Add-Content -Value "<tr><td>webApp</td><td>$currentAppId</td><td><a href='$webAppPortalUrl'>WebApp-OpenIDConnect-DotNet-code-v2</a></td></tr>" -Path createdApps.html
214214

215215
$requiredResourcesAccess = New-Object System.Collections.Generic.List[Microsoft.Open.AzureAD.Model.RequiredResourceAccess]
216216

217217
# Add Required Resources Access (from 'webApp' to 'Microsoft Graph')
218218
Write-Host "Getting access from 'webApp' to 'Microsoft Graph'"
219219
$requiredPermissions = GetRequiredPermissions -applicationDisplayName "Microsoft Graph" `
220-
-requiredDelegatedPermissions "User.Read";
220+
-requiredDelegatedPermissions "User.Read" `
221221

222222
$requiredResourcesAccess.Add($requiredPermissions)
223223

@@ -226,9 +226,9 @@ Function ConfigureApplications
226226
Write-Host "Granted permissions."
227227

228228
# Update config file for 'webApp'
229-
$configFile = $pwd.Path + "\..\active-directory-aspnetcore-webapp-openidconnect-v2\appsettings.json"
229+
$configFile = $pwd.Path + "\..\appsettings.json"
230230
Write-Host "Updating the sample code ($configFile)"
231-
$dictionary = @{ "ClientId" = $webAppAadApplication.AppId;"TenantId" = $tenantId;"Domain" = $tenantName;"TenantId" = $webAppAppKey };
231+
$dictionary = @{ "ClientId" = $webAppAadApplication.AppId;"TenantId" = $tenantId;"Domain" = $tenantName;"ClientSecret" = $webAppAppKey };
232232
UpdateTextFile -configFilePath $configFile -dictionary $dictionary
233233

234234
Add-Content -Value "</tbody></table></body></html>" -Path createdApps.html

2. WebApp calls Microsoft Graph on behalf of signed-in user/2.1. using authorization code flow/AppCreationScripts/sample.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"Sample": {
3-
"Title": "An ASP.NET Core 2.x Web App which lets sign-in users with work and school or Microsoft personal accounts (and calls Microsoft Graph)",
3+
"Title": "Using the Microsoft identity platform to call the Microsoft Graph API from an An ASP.NET Core 2.x Web App, on behalf of a user signing-in using their work and school or Microsoft personal account",
44
"Level": 200,
55
"Client": "ASP.NET Core 2.x Web App",
66
"Service": "Microsoft Graph",
@@ -14,7 +14,7 @@
1414
"AADApps": [
1515
{
1616
"Id": "webApp",
17-
"Name": "WebApp",
17+
"Name": "WebApp-OpenIDConnect-DotNet-code-v2",
1818
"Kind": "WebApp",
1919
"HomePage": "https://localhost:44321/",
2020
"ReplyUrls": "https://localhost:44321/, https://localhost:44321/signin-oidc",
@@ -39,7 +39,7 @@
3939
{
4040
"App": "webApp",
4141
"SettingKind": "JSon",
42-
"SettingFile": "\\..\\active-directory-aspnetcore-webapp-openidconnect-v2\\appsettings.json",
42+
"SettingFile": "\\..\\appsettings.json",
4343
"Mappings": [
4444
{
4545
"key": "ClientId",
@@ -54,7 +54,7 @@
5454
"value": "$tenantName"
5555
},
5656
{
57-
"key": "TenantId",
57+
"key": "ClientSecret",
5858
"value": ".AppKey"
5959
}
6060
]

2. WebApp calls Microsoft Graph on behalf of signed-in user/2.1. using authorization code flow/README.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ client: ASP.NET Core 2.x Web App
77
service: Microsoft Graph
88
endpoint: AAD v2.0
99
---
10-
# Using the Microsoft identity platform to call the Microsoft Graph API from a Web App, on behalf of the signed-in user.
10+
11+
# Using the Microsoft identity platform to call the Microsoft Graph API from an An ASP.NET Core 2.x Web App, on behalf of a user signing-in using their work and school or Microsoft personal account
1112

1213
![Build badge](https://identitydivision.visualstudio.com/_apis/public/build/definitions/a7934fdd-dcde-4492-a406-7fad6ac00e17/514/badge)
1314

1415
## Scenario
1516

16-
Starting from a .NET Core 2.2 MVC Web app that uses OpenID Connect to sign in users, this phase of the tutorial shows how to calls the Microsoft Graph me endpoint on behalf of the signed-in user. It leverages the ASP.NET Core OpenID Connect middleware and Microsoft Authentication Library for .NET (MSAL.NET). Their complexities where encapsultated into the `Microsoft.Identity.Web` reusable library project part of this tutorial. Once again the notion of ASP.NET services injected by dependency injection is heavily used.
17+
Starting from a .NET Core 2.2 MVC Web app that uses OpenID Connect to sign in users, this phase of the tutorial shows how to call Microsoft Graph /me endpoint on behalf of the signed-in user. It leverages the ASP.NET Core OpenID Connect middleware and Microsoft Authentication Library for .NET (MSAL.NET). Their complexities where encapsultated into the `Microsoft.Identity.Web` reusable library project part of this tutorial. Once again the notion of ASP.NET services injected by dependency injection is heavily used.
1718

1819
![Sign in with the Microsoft identity platform for developers (fomerly Azure AD v2.0)](ReadmeFiles/sign-in.png)
1920

@@ -27,9 +28,9 @@ To run this sample:
2728
2829
### Step 1: Register the sample with your Azure AD tenant
2930

30-
You first need to have [registered](https://github.com/Azure-Samples/active-directory-aspnetcore-webapp-openidconnect-v2/tree/aspnetcore2-2#step-1-register-the-sample-with-your-azure-ad-tenant) your app as described in [the first tutorial](https://github.com/Azure-Samples/active-directory-aspnetcore-webapp-openidconnect-v2/tree/aspnetcore2-2)
31+
You first need to [register](https://github.com/Azure-Samples/active-directory-aspnetcore-webapp-openidconnect-v2/tree/aspnetcore2-2#step-1-register-the-sample-with-your-azure-ad-tenant) your app as described in [the first tutorial](https://github.com/Azure-Samples/active-directory-aspnetcore-webapp-openidconnect-v2/tree/aspnetcore2-2)
3132

32-
Then here are the extra steps:
33+
Then follow the following extra set of steps:
3334

3435
1. From the **Certificates & secrets** page, for your app registration, in the **Client secrets** section, choose **New client secret**:
3536

@@ -76,9 +77,9 @@ Go to the `"2. WebApp calls Microsoft Graph on behalf of signed-in use/2.1. usin
7677

7778
## About The code
7879

79-
Starting from the [previous phase of the tutorial](../../1.%20WebApp%20signs-in%20users%20with%20Microsoft%20Identity%20(OIDC)), the code was incrementally updated by following these steps:
80+
Starting from the [previous phase of the tutorial](../../1.%20WebApp%20signs-in%20users%20with%20Microsoft%20Identity%20(OIDC)), the code was incrementally updated with the following steps:
8081

81-
### Update the `Startup.cs` file to enable TokenAcquisition MSAL.NET based service
82+
### Update the `Startup.cs` file to enable TokenAcquisition by a MSAL.NET based service
8283

8384
After the following lines in the ConfigureServices(IServiceCollection services) method, replace `services.AddAzureAdV2Authentication(Configuration);`, by the following lines:
8485

@@ -96,12 +97,12 @@ After the following lines in the ConfigureServices(IServiceCollection services)
9697

9798
The two new lines of code:
9899

99-
- enable MSAL.NET to hook-up to the OpenID Connect events and redeem the authorization code obtained by the ASP.NET Core middleware and get a token into the token cache, for use by the Controllers.
100+
- enable MSAL.NET to hook-up to the OpenID Connect events and redeem the authorization code obtained by the ASP.NET Core middleware and after obtaining a token, saves it into the token cache, for use by the Controllers.
100101
- Decide which token cache implementation to use. In this part of the phase, we'll use a simple in memory token cache, but next steps will show you other implementations you can benefit from, including distributed token caches based on a SQL database, or a Redis cache.
101102

102103
### Add additional files to call Microsoft Graph
103104

104-
Add the `Services\Microsoft-Graph-Rest\*.cs` files. This is an implementation of a custom service which encapsultes the call to the Microsoft Graph me endpoint. Given an access token to access the Microsoft Graph, it's capable of getting the user information and the photo of the user.
105+
Add the `Services\Microsoft-Graph-Rest\*.cs` files. This is an implementation of a custom service which encapsultes the call to the Microsoft Graph /me endpoint. Given an access token for Microsoft Graph, it's capable of getting the user information and the photo of the user.
105106

106107
```CSharp
107108
public interface IGraphApiOperations
@@ -113,7 +114,7 @@ public interface IGraphApiOperations
113114

114115
### Update the `Startup.cs` file to enable the Microsoft Graph custom service
115116

116-
Still in the `Startup.cs` file, add the following lines just after the following. This lines ensures that the GraphAPIService benefits from the optimized `HttpClient` management by ASP.NET Core
117+
Still in the `Startup.cs` file, add the following lines just after the following. This lines ensures that the GraphAPIService benefits from the optimized `HttpClient` management by ASP.NET Core.
117118

118119
```CSharp
119120
// Add Graph
@@ -213,5 +214,6 @@ HTML table displaying the properties of the *me* object as returned by Microsoft
213214
- 3rd party, or even [your own Web API](../../4.%20WebApp%20calls%20your%20own%20Web%20API), which will enable you to learn about custom scopes
214215

215216
## Learn more
217+
216218
- Learn how [Microsoft.Identity.Web](../../Microsoft.Identity.Web) works, in particular hooks-up to the ASP.NET Core ODIC events
217219
- [Use HttpClientFactory to implement resilient HTTP requests](https://docs.microsoft.com/en-us/dotnet/standard/microservices-architecture/implement-resilient-applications/use-httpclientfactory-to-implement-resilient-http-requests) used by the Graph custom service

0 commit comments

Comments
 (0)