Skip to content

Commit 709dafe

Browse files
author
Kalyan Krishna
committed
Updated the groups sample a bit
Tested the Session token provider and added some code to implement and test it Created two separate readme Proposed a new method name Removed Sessiontokencache from WebApi as it cannot be used in a webapi which has no cookies available. Removed the older SAML claim mapping to keep samples stick to the new claims only.
1 parent ae80827 commit 709dafe

File tree

12 files changed

+653
-66
lines changed

12 files changed

+653
-66
lines changed

4-WebApp-your-API/Client/Startup.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Microsoft.Extensions.DependencyInjection;
99
using Microsoft.Identity.Web;
1010
using Microsoft.Identity.Web.Client.TokenCacheProviders;
11+
using System.IdentityModel.Tokens.Jwt;
1112
using TodoListClient.Services;
1213

1314
namespace WebApp_OpenIDConnect_DotNet
@@ -35,6 +36,12 @@ public void ConfigureServices(IServiceCollection services)
3536

3637
services.AddOptions();
3738

39+
// This is required to be instantiated before the OpenIdConnectOptions starts getting configured.
40+
// By default, the claims mapping will map claim names in the old format to accommodate older SAML applications.
41+
// 'http://schemas.microsoft.com/ws/2008/06/identity/claims/role' instead of 'roles'
42+
// This flag ensures that the ClaimsIdentity claims collection will be built from the claims in the token
43+
JwtSecurityTokenHandler.DefaultMapInboundClaims = false;
44+
3845
// Token acquisition service based on MSAL.NET
3946
// and chosen token cache implementation
4047
services.AddAzureAdV2Authentication(Configuration)

4-WebApp-your-API/TodoListService/Startup.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2929
using Microsoft.Extensions.DependencyInjection;
3030
using Microsoft.Identity.Web;
3131
using Microsoft.Identity.Web.Client.TokenCacheProviders;
32+
using System.IdentityModel.Tokens.Jwt;
3233

3334
namespace TodoListService
3435
{
@@ -44,6 +45,12 @@ public Startup(IConfiguration configuration)
4445
// This method gets called by the runtime. Use this method to add services to the container.
4546
public void ConfigureServices(IServiceCollection services)
4647
{
48+
// This is required to be instantiated before the OpenIdConnectOptions starts getting configured.
49+
// By default, the claims mapping will map claim names in the old format to accommodate older SAML applications.
50+
// 'http://schemas.microsoft.com/ws/2008/06/identity/claims/role' instead of 'roles'
51+
// This flag ensures that the ClaimsIdentity claims collection will be built from the claims in the token
52+
JwtSecurityTokenHandler.DefaultMapInboundClaims = false;
53+
4754
services.AddProtectWebApiWithMicrosoftIdentityPlatformV2(Configuration)
4855
.AddInMemoryTokenCaches();
4956

5-WebApp-AuthZ/5-2-Groups/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-GroupClaims) if needed"
53-
$app=Get-AzureADApplication -Filter "DisplayName eq 'WebApp-GroupClaims'"
53+
Get-AzureADApplication -Filter "DisplayName eq 'WebApp-GroupClaims'" | ForEach-Object {Remove-AzureADApplication -ObjectId $_.ObjectId }
54+
$apps = Get-AzureADApplication -Filter "DisplayName eq 'WebApp-GroupClaims'"
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-GroupClaims.."
5964
}
60-
65+
# also remove service principals of this app
66+
Get-AzureADServicePrincipal -filter "DisplayName eq 'WebApp-GroupClaims'" | ForEach-Object {Remove-AzureADServicePrincipal -ObjectId $_.Id -Confirm:$false}
67+
6168
}
6269

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

5-WebApp-AuthZ/5-2-Groups/AppCreationScripts/Configure.ps1

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ Function ConfigureApplications
145145
so that they are consistent with the Applications parameters
146146
#>
147147

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

@@ -201,8 +203,8 @@ Function ConfigureApplications
201203
$owner = Get-AzureADApplicationOwner -ObjectId $webAppAadApplication.ObjectId
202204
if ($owner -eq $null)
203205
{
204-
Add-AzureADApplicationOwner -ObjectId $webAppAadApplication.ObjectId -RefObjectId $user.ObjectId
205-
Write-Host "'$($user.UserPrincipalName)' added as an application owner to app '$($webAppServicePrincipal.DisplayName)'"
206+
Add-AzureADApplicationOwner -ObjectId $webAppAadApplication.ObjectId -RefObjectId $user.ObjectId
207+
Write-Host "'$($user.UserPrincipalName)' added as an application owner to app '$($webAppServicePrincipal.DisplayName)'"
206208
}
207209

208210
Write-Host "Done creating the webApp application (WebApp-GroupClaims)"
@@ -230,7 +232,7 @@ Function ConfigureApplications
230232
Write-Host "Updating the sample code ($configFile)"
231233
$dictionary = @{ "ClientId" = $webAppAadApplication.AppId;"TenantId" = $tenantId;"Domain" = $tenantName;"ClientSecret" = $webAppAppKey };
232234
UpdateTextFile -configFilePath $configFile -dictionary $dictionary
233-
235+
234236
Add-Content -Value "</tbody></table></body></html>" -Path createdApps.html
235237
}
236238

0 commit comments

Comments
 (0)