Skip to content

Commit d5166bb

Browse files
authored
Preparing Microsoft.Identity.Web for being disributed as a NuGet package (#153)
* Preparing Microsoft.Identity.Web for being disributed as a NuGet package 1) Merging the content from microsoft-authentication-extensions-for-dotnet Web 2) Aligning the namespaces with the folder strucuture 3) Renaming files and classes per the style used in MSAL.NET * Improving the doc * Updating the README.md * Adding an architecture diagram * Updating the code * Enabling the creation of a NuGet package * Fixing regression introduced when merging both code bases * 1. Added CreateTokenCachingTablesInSqlDatabase in SqlTokenCacheProviderExtension.cs as the developer can no longer uncomment a piece of code to create tables for Sql token caching. Updated 2-2-Token-caching 2. Fixed a bug in 4-WebApp-Your-Api which needed **services.AddSession();** in AddProtectWebApiWithMicrosoftIdentityPlatformV2(). 3. Added "JwtSecurityTokenHandler.DefaultMapInboundClaims = false;" in a few startup.cs to encourage using the new claim set in V2 samples. 4. Lots of comments updated in Microsoft.IDentity.Web classes 5. Renamed *ServiceCollectionExtension classes to *TokenCacheProviderExtensions as the classes have extention methods for token providers and the name matches the purpose. 6. Fixed a bug in MsalAppMemoryTokenCacheProvider 7. Remove redundant parameters from IMsalAppTokenCacheProvider and IMsalUserTokenCacheProvider Initialize() methods * minor typo fixed * Removing, `JwtSecurityTokenHandler.DefaultMapInboundClaims = false;` because `preferred_username` claim was not being mapped * Revert "Removing, `JwtSecurityTokenHandler.DefaultMapInboundClaims = false;` because `preferred_username` claim was not being mapped" This reverts commit 45baf51. * Change AddProtectWebApiWithMicrosoftIdentityPlatformV2 by AddProtectedWebApiWithMicrosoftIdentityPlatformV2 * Acting on @MarkZuber 's PR feedback * AddAzureAdV2Authentication, AddProtectedApiCallsWebApis; AddMsal now takes the configuration (and optional parameters to let the developer choose the options section) => all the Startup.cs file using it are modified. Simplifying the implementation of the token cache serializers by creating a IMsalTokenCacheProvider interface and a MsalAbstractTokenCacheProvider class implementing the common code in all the token cache providers. Fixing a few code smells (including ConfigureAwait(false) where needed. Chaning the CallGraph solution to be a Visual Studio 2019 solution. * Addressing Mark's additional suggestions * Updating the token caches * Renaming the AbstractMsalTokenCacheProvider into MsaAbstractTokenCacheProvider, adding a class diagram and improving a README.md * - Renamed AddAzureAdV2Authentication into AddMicrosoftIdentityPlatformAuthentication - Renamed AddProtectedWebApiWithMicrosoftIdentityPlatformV2 into AddProtectedWebApi * Provide migration help for the Sql Token cache provider Remove the AsyncUsageAnalyzers Improves the build.bat and buildAllSln.proj to add more target (restore and clean) * Fixing a merged project * Renaming MsalUiRequiredExceptionFilterAttribute to AuthorizeForScopesAttribute
1 parent a2d1520 commit d5166bb

File tree

107 files changed

+4058
-2563
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

107 files changed

+4058
-2563
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,8 @@
9696
/4-WebApp-your-API/TodoListService/obj
9797
/4-WebApp-your-API/TodoListService/bin
9898
/4-WebApp-your-API/Client/obj
99+
/2-WebApp-graph-user/2-4-Sovereign-Call-MSGraph/bin/Debug/netcoreapp2.2
100+
/2-WebApp-graph-user/2-4-Sovereign-Call-MSGraph/obj
101+
/2-WebApp-graph-user/2-4-Sovereign-Call-MSGraph/bin/Release/netcoreapp2.2
102+
/Microsoft.Identity.Web.Test/bin/Release/netcoreapp2.2
103+
/Microsoft.Identity.Web.Test/obj

1-WebApp-OIDC/1-1-MyOrg/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ cd "1-WebApp-OIDC\1-1-MyOrg"
136136
by this line:
137137
138138
```CSharp
139-
services.AddAzureAdV2Authentication(Configuration);
139+
services.AddMicrosoftIdentityPlatformAuthentication(Configuration);
140140
```
141141
142142
This enables your application to use the Microsoft identity platform endpoint. This endpoint is capable of signing-in users both with their Work and School and Microsoft Personal accounts.

1-WebApp-OIDC/1-1-MyOrg/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public void ConfigureServices(IServiceCollection services)
3131
});
3232

3333
// Sign-in users with the Microsoft identity platform
34-
services.AddAzureAdV2Authentication(Configuration);
34+
services.AddMicrosoftIdentityPlatformAuthentication(Configuration);
3535

3636
services.AddMvc(options =>
3737
{

1-WebApp-OIDC/1-2-AnyOrg/README-1-1-to-1-2.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,14 @@ The actual sign-in audience (accounts to sign-in) is the lowest set of what is s
4848

4949
In order to restrict users from specific organizations to sign-in to your web app, you'll need to follow the steps above, and customize a bit more the code to restrict the valid token issuers. The token issuers are really the tenanted Azure AD authority which are allowed to issue a token to access your web application.
5050

51-
In the `Startup.cs` file, in the `ConfigureServices` method, after `services.AddAzureAdV2Authentication(Configuration)` add some code to validate specific issuers by overriding the `TokenValidationParameters.IssuerValidator` delegate.
51+
In the `Startup.cs` file, in the `ConfigureServices` method, after `services.AddMicrosoftIdentityPlatformAuthentication(Configuration)` add some code to validate specific issuers by overriding the `TokenValidationParameters.IssuerValidator` delegate.
5252

5353
```CSharp
5454
public void ConfigureServices(IServiceCollection services)
5555
{
5656
...
5757
// Sign-in users with the Microsoft identity platform
58-
services.AddAzureAdV2Authentication(Configuration);
58+
services.AddMicrosoftIdentityPlatformAuthentication(Configuration);
5959

6060
// Restrict users to specific belonging to specific tenants
6161
services.Configure<OpenIdConnectOptions>(AzureADDefaults.OpenIdScheme, options =>

1-WebApp-OIDC/1-2-AnyOrg/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ cd "1-WebApp-OIDC\1-2-AnyOrg"
136136
by this line:
137137
138138
```CSharp
139-
services.AddAzureAdV2Authentication(Configuration);
139+
services.AddMicrosoftIdentityPlatformAuthentication(Configuration);
140140
```
141141
142142
This enables your application to use the Microsoft identity platform endpoint. This endpoint is capable of signing-in users both with their Work and School and Microsoft Personal accounts.

1-WebApp-OIDC/1-2-AnyOrg/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public void ConfigureServices(IServiceCollection services)
3131
});
3232

3333
// Sign-in users with the Microsoft identity platform
34-
services.AddAzureAdV2Authentication(Configuration);
34+
services.AddMicrosoftIdentityPlatformAuthentication(Configuration);
3535

3636
services.AddMvc(options =>
3737
{

1-WebApp-OIDC/1-3-AnyOrgOrPersonal/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ In the **appsettings.json** file:
150150
by this line:
151151
152152
```CSharp
153-
services.AddAzureAdV2Authentication(Configuration);
153+
services.AddMicrosoftIdentityPlatformAuthentication(Configuration);
154154
```
155155
156156
This enables your application to use the Microsoft identity platform endpoint. This endpoint is capable of signing-in users both with their Work and School and Microsoft Personal accounts.

1-WebApp-OIDC/1-3-AnyOrgOrPersonal/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public void ConfigureServices(IServiceCollection services)
3131
});
3232

3333
// Sign-in users with the Microsoft identity platform
34-
services.AddAzureAdV2Authentication(Configuration);
34+
services.AddMicrosoftIdentityPlatformAuthentication(Configuration);
3535

3636
services.AddMvc(options =>
3737
{

1-WebApp-OIDC/1-4-Sovereign/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ cd "1-WebApp-OIDC\1-4-Sovereign"
124124
by this line:
125125
126126
```CSharp
127-
services.AddAzureAdV2Authentication(Configuration);
127+
services.AddMicrosoftIdentityPlatformAuthentication(Configuration);
128128
```
129129
130130
This enables your application to use the Microsoft identity platform endpoint. This endpoint is capable of signing-in users both with their Work and School and Microsoft Personal accounts.

1-WebApp-OIDC/1-4-Sovereign/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public void ConfigureServices(IServiceCollection services)
3131
});
3232

3333
// Sign-in users with the Microsoft identity platform
34-
services.AddAzureAdV2Authentication(Configuration);
34+
services.AddMicrosoftIdentityPlatformAuthentication(Configuration);
3535

3636
services.AddMvc(options =>
3737
{

1-WebApp-OIDC/1-6-SignOut/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ from <https://github.com/aspnet/AspNetCore/blob/master/src/Azure/AzureAD/Authent
5656
The ASP.NET Core OpenIdConnect middleware enables your app to intercept the call to the Microsoft identity platform logout endpoint by providing an OpenIdConnect event named `OnRedirectToIdentityProviderForSignOut`.
5757

5858
```CSharp
59-
public static IServiceCollection AddAzureAdV2Authentication(this IServiceCollection services,
59+
public static IServiceCollection AddMicrosoftIdentityPlatformAuthentication(this IServiceCollection services,
6060
IConfiguration configuration)
6161
{
6262
services.Configure<OpenIdConnectOptions>(AzureADDefaults.OpenIdScheme, options =>

2-WebApp-graph-user/2-1-Call-MSGraph/AspnetCoreWebApp-calls-Microsoft-Graph.sln

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 15
4-
VisualStudioVersion = 15.0.27130.2027
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.29123.89
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebApp-OpenIDConnect-DotNet", "WebApp-OpenIDConnect-DotNet.csproj", "{8DCFEEC2-0A85-4C7E-B96A-21C9184470B1}"
77
EndProject

2-WebApp-graph-user/2-1-Call-MSGraph/Controllers/HomeController.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
using Microsoft.AspNetCore.Mvc;
77
using Microsoft.Extensions.Options;
88
using Graph=Microsoft.Graph;
9-
using Microsoft.Identity.Web.Client;
9+
using Microsoft.Identity.Web;
1010
using WebApp_OpenIDConnect_DotNet.Infrastructure;
1111
using WebApp_OpenIDConnect_DotNet.Models;
1212
using WebApp_OpenIDConnect_DotNet.Services;
@@ -31,7 +31,7 @@ public IActionResult Index()
3131
return View();
3232
}
3333

34-
[MsalUiRequiredExceptionFilter(Scopes = new[] { Constants.ScopeUserRead })]
34+
[AuthorizeForScopes(Scopes = new[] { Constants.ScopeUserRead })]
3535
public async Task<IActionResult> Profile()
3636
{
3737
// Initialize the GraphServiceClient.
@@ -43,9 +43,11 @@ public async Task<IActionResult> Profile()
4343
try
4444
{
4545
// Get user photo
46-
var photoStream = await graphClient.Me.Photo.Content.Request().GetAsync();
47-
byte[] photoByte = ((MemoryStream)photoStream).ToArray();
48-
ViewData["Photo"] = Convert.ToBase64String(photoByte);
46+
using (var photoStream = await graphClient.Me.Photo.Content.Request().GetAsync())
47+
{
48+
byte[] photoByte = ((MemoryStream)photoStream).ToArray();
49+
ViewData["Photo"] = Convert.ToBase64String(photoByte);
50+
}
4951
}
5052
catch (System.Exception)
5153
{
@@ -59,8 +61,7 @@ public async Task<IActionResult> Profile()
5961
{
6062
return GraphServiceClientFactory.GetAuthenticatedGraphClient(async () =>
6163
{
62-
string result = await tokenAcquisition.GetAccessTokenOnBehalfOfUser(
63-
HttpContext, scopes);
64+
string result = await tokenAcquisition.GetAccessTokenOnBehalfOfUserAsync(scopes);
6465
return result;
6566
}, webOptions.GraphApiUrl);
6667
}

2-WebApp-graph-user/2-1-Call-MSGraph/README.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,16 @@ Starting from the [previous phase of the tutorial](../../1-WebApp-OIDC), the cod
8282

8383
### Update the `Startup.cs` file to enable TokenAcquisition by a MSAL.NET based service
8484

85-
After the following lines in the ConfigureServices(IServiceCollection services) method, replace `services.AddAzureAdV2Authentication(Configuration);`, by the following lines:
85+
After the following lines in the ConfigureServices(IServiceCollection services) method, replace `services.AddMicrosoftIdentityPlatformAuthentication(Configuration);`, by the following lines:
8686

8787
```CSharp
8888
public void ConfigureServices(IServiceCollection services)
8989
{
9090
. . .
9191
// Token acquisition service based on MSAL.NET
9292
// and chosen token cache implementation
93-
services.AddAzureAdV2Authentication(Configuration)
94-
.AddMsal(new string[] { Constants.ScopeUserRead })
93+
services.AddMicrosoftIdentityPlatformAuthentication(Configuration)
94+
.AddMsal(Configuration, new string[] { Constants.ScopeUserRead })
9595
.AddInMemoryTokenCache();
9696
```
9797

@@ -100,9 +100,14 @@ The two new lines of code:
100100
- 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.
101101
- 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.
102102

103+
> Note that you can replace the *in memory token cache* serialization by a *session token cache* (stored in a session cookie). To do this replacement, change the following in **Startup.cs**:
104+
> - replace `using Microsoft.Identity.Web.TokenCacheProviders.InMemory` by `using Microsoft.Identity.Web.TokenCacheProviders.Session`
105+
> - Replace `.AddInMemoryTokenCaches()` by `.AddSessionTokenCaches()`
106+
> add `app.UseSession();` in the `Configure(IApplicationBuilder app, IHostingEnvironment env)` method, for instance after `app.UseCookiePolicy();`
107+
103108
### Add additional files to call Microsoft Graph
104109

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.
110+
Add the `Services\Microsoft-Graph-Rest\*.cs` files. This is an implementation of a custom service which encapsulates 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.
106111

107112
```CSharp
108113
public interface IGraphApiOperations
@@ -138,10 +143,10 @@ In the `Controllers\HomeController.cs`file:
138143
private readonly IGraphApiOperations graphApiOperations;
139144
```
140145

141-
1. Add a `Profile()` action so that it calls the Microsoft Graph *me* endpoint. In case a token cannot be acquired, a challenge is attempted to re-sign-in the user, and have them consent to the requested scopes. This is expressed declaratively by the `MsalUiRequiredExceptionFilter`attribute. This attribute is part of the `Microsoft.Identity.Web` project and automatically manages incremental consent.
146+
1. Add a `Profile()` action so that it calls the Microsoft Graph *me* endpoint. In case a token cannot be acquired, a challenge is attempted to re-sign-in the user, and have them consent to the requested scopes. This is expressed declaratively by the `AuthorizeForScopes`attribute. This attribute is part of the `Microsoft.Identity.Web` project and automatically manages incremental consent.
142147

143148
```CSharp
144-
[MsalUiRequiredExceptionFilter(Scopes = new[] {Constants.ScopeUserRead})]
149+
[AuthorizeForScopes(Scopes = new[] {Constants.ScopeUserRead})]
145150
public async Task<IActionResult> Profile()
146151
{
147152
var accessToken =

2-WebApp-graph-user/2-1-Call-MSGraph/Services/GraphServiceClientFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public CustomAuthenticationProvider(Func<Task<string>> acquireTokenCallback)
2323
acquireAccessToken = acquireTokenCallback;
2424
}
2525

26-
private Func<Task<string>> acquireAccessToken;
26+
private readonly Func<Task<string>> acquireAccessToken;
2727

2828
public async Task AuthenticateRequestAsync(HttpRequestMessage request)
2929
{

2-WebApp-graph-user/2-1-Call-MSGraph/Startup.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
using Microsoft.Extensions.Configuration;
88
using Microsoft.Extensions.DependencyInjection;
99
using Microsoft.Identity.Web;
10-
using Microsoft.Identity.Web.Client.TokenCacheProviders;
10+
using Microsoft.Identity.Web.TokenCacheProviders.InMemory;
1111
using WebApp_OpenIDConnect_DotNet.Infrastructure;
1212
using WebApp_OpenIDConnect_DotNet.Services;
1313

@@ -36,9 +36,9 @@ public void ConfigureServices(IServiceCollection services)
3636

3737
// Token acquisition service based on MSAL.NET
3838
// and chosen token cache implementation
39-
services.AddAzureAdV2Authentication(Configuration)
40-
.AddMsal(new string[] { Constants.ScopeUserRead })
41-
.AddInMemoryTokenCaches();
39+
services.AddMicrosoftIdentityPlatformAuthentication(Configuration)
40+
.AddMsal(Configuration, new string[] { Constants.ScopeUserRead })
41+
.AddInMemoryTokenCaches();
4242

4343
// Add Graph
4444
services.AddGraphService(Configuration);

2-WebApp-graph-user/2-2-TokenCache/AppCreationScripts/Cleanup.ps1

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ param(
55
[string] $tenantId
66
)
77

8-
if ((Get-Module -ListAvailable -Name "AzureAD") -eq $null) {
8+
if ($null -eq (Get-Module -ListAvailable -Name "AzureAD")) {
99
Install-Module "AzureAD" -Scope CurrentUser
1010
}
1111
Import-Module AzureAD
@@ -44,20 +44,27 @@ This function removes the Azure AD applications for the sample. These applicatio
4444
$tenantId = $creds.Tenant.Id
4545
}
4646
$tenant = Get-AzureADTenantDetail
47-
$tenantName = ($tenant.VerifiedDomains | Where { $_._Default -eq $True }).Name
47+
$tenantName = ($tenant.VerifiedDomains | Where-Object { $_._Default -eq $True }).Name
4848

4949
# Removes the applications
5050
Write-Host "Cleaning-up applications from tenant '$tenantName'"
5151

5252
Write-Host "Removing 'webApp' (WebApp-OpenIDConnect-DotNet-code-v2) if needed"
53-
$app=Get-AzureADApplication -Filter "DisplayName eq 'WebApp-OpenIDConnect-DotNet-code-v2'"
53+
Get-AzureADApplication -Filter "DisplayName eq 'WebApp-OpenIDConnect-DotNet-code-v2'" | ForEach-Object {Remove-AzureADApplication -ObjectId $_.ObjectId }
54+
$apps = Get-AzureADApplication -Filter "DisplayName eq 'WebApp-OpenIDConnect-DotNet-code-v2'"
55+
if ($apps)
56+
{
57+
Remove-AzureADApplication -ObjectId $apps.ObjectId
58+
}
5459

55-
if ($app)
60+
foreach ($app in $apps)
5661
{
5762
Remove-AzureADApplication -ObjectId $app.ObjectId
58-
Write-Host "Removed."
63+
Write-Host "Removed WebApp-OpenIDConnect-DotNet-code-v2.."
5964
}
60-
65+
# also remove service principals of this app
66+
Get-AzureADServicePrincipal -filter "DisplayName eq 'WebApp-OpenIDConnect-DotNet-code-v2'" | ForEach-Object {Remove-AzureADServicePrincipal -ObjectId $_.Id -Confirm:$false}
67+
6168
}
6269

63-
Cleanup -Credential $Credential -tenantId $TenantId
70+
Cleanup -Credential $Credential -tenantId $TenantId

2-WebApp-graph-user/2-2-TokenCache/AppCreationScripts/Configure.ps1

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ Function UpdateTextFile([string] $configFilePath, [System.Collections.HashTable]
134134
Set-Content -Path $configFilePath -Value $lines -Force
135135
}
136136

137+
137138
Set-Content -Value "<html><body><table>" -Path createdApps.html
138139
Add-Content -Value "<thead><tr><th>Application</th><th>AppId</th><th>Url in the Azure portal</th></tr></thead><tbody>" -Path createdApps.html
139140

@@ -145,6 +146,8 @@ Function ConfigureApplications
145146
so that they are consistent with the Applications parameters
146147
#>
147148

149+
$commonendpoint = "common"
150+
148151
# $tenantId is the Active Directory Tenant. This is a GUID which represents the "Directory ID" of the AzureAD tenant
149152
# into which you want to create the apps. Look it up in the Azure portal in the "Properties" of the Azure AD.
150153

@@ -201,10 +204,11 @@ Function ConfigureApplications
201204
$owner = Get-AzureADApplicationOwner -ObjectId $webAppAadApplication.ObjectId
202205
if ($owner -eq $null)
203206
{
204-
Add-AzureADApplicationOwner -ObjectId $webAppAadApplication.ObjectId -RefObjectId $user.ObjectId
205-
Write-Host "'$($user.UserPrincipalName)' added as an application owner to app '$($webAppServicePrincipal.DisplayName)'"
207+
Add-AzureADApplicationOwner -ObjectId $webAppAadApplication.ObjectId -RefObjectId $user.ObjectId
208+
Write-Host "'$($user.UserPrincipalName)' added as an application owner to app '$($webAppServicePrincipal.DisplayName)'"
206209
}
207210

211+
208212
Write-Host "Done creating the webApp application (WebApp-OpenIDConnect-DotNet-code-v2)"
209213

210214
# URL of the AAD application in the Azure portal
@@ -230,7 +234,7 @@ Function ConfigureApplications
230234
Write-Host "Updating the sample code ($configFile)"
231235
$dictionary = @{ "ClientId" = $webAppAadApplication.AppId;"TenantId" = $tenantId;"Domain" = $tenantName;"ClientSecret" = $webAppAppKey };
232236
UpdateTextFile -configFilePath $configFile -dictionary $dictionary
233-
237+
234238
Add-Content -Value "</tbody></table></body></html>" -Path createdApps.html
235239
}
236240

2-WebApp-graph-user/2-2-TokenCache/Controllers/HomeController.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using System.Threading.Tasks;
33
using Microsoft.AspNetCore.Authorization;
44
using Microsoft.AspNetCore.Mvc;
5-
using Microsoft.Identity.Web.Client;
5+
using Microsoft.Identity.Web;
66
using WebApp_OpenIDConnect_DotNet.Infrastructure;
77
using WebApp_OpenIDConnect_DotNet.Models;
88
using WebApp_OpenIDConnect_DotNet.Services.GraphOperations;
@@ -27,11 +27,11 @@ public IActionResult Index()
2727
return View();
2828
}
2929

30-
[MsalUiRequiredExceptionFilter(Scopes = new[] {Constants.ScopeUserRead})]
30+
[AuthorizeForScopes(Scopes = new[] {Constants.ScopeUserRead})]
3131
public async Task<IActionResult> Profile()
3232
{
3333
var accessToken =
34-
await tokenAcquisition.GetAccessTokenOnBehalfOfUser(HttpContext, new[] {Constants.ScopeUserRead});
34+
await tokenAcquisition.GetAccessTokenOnBehalfOfUserAsync(new[] {Constants.ScopeUserRead});
3535

3636
var me = await graphApiOperations.GetUserInformation(accessToken);
3737
var photo = await graphApiOperations.GetPhotoAsBase64Async(accessToken);

2-WebApp-graph-user/2-2-TokenCache/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,9 @@ Go to the `"2-WebApp-graph-user\2-2-TokenCache"` folder
6060
> "TokenCacheDbConnStr": "Data Source=(LocalDb)\\MSSQLLocalDB;Database=MY_TOKEN_CACHE_DATABASE;Trusted_Connection=True;"
6161
> },
6262
> ```
63+
6364
1. If you do not have an existing database and tables needed for token caching, this sample can use [EF Core- code first](https://docs.microsoft.com/en-us/ef/core/get-started/aspnetcore/new-db?tabs=visual-studio) to create a database and tables for you. to do that, follow the steps below.
64-
1. In the file `Microsoft.Identity.Web\Client\TokenCacheProviders\Sql\MSALAppSqlTokenCacheProviderExtension.cs`, uncomment the code under the **// Uncomment the following lines to create the database.**. This comment exists once in the **AddSqlAppTokenCache** and **AddSqlPerUserTokenCache** methods.
65+
1. In the file `Startup.cs`, uncomment the code under the **// Uncomment the following to initialize the sql server database with tables required to cache tokens.**. This comment exists once in the **ConfigureServices** methods.
6566
1. Run the solution again, when a user signs-in the very first time, the Entity Framework will create the database and tables `AppTokenCache` and `UserTokenCache` for app and user token caching respectively.
6667
6768
- In case you want to deploy your app in Sovereign or national clouds, ensure the `GraphApiUrl` option matches the one you want. By default this is Microsoft Graph in the Azure public cloud

0 commit comments

Comments
 (0)