5
5
using Microsoft . Extensions . DependencyInjection ;
6
6
using Microsoft . Identity . Web . Client ;
7
7
using Microsoft . Identity . Web . Resource ;
8
+ using Microsoft . IdentityModel . Tokens ;
8
9
using System . Collections . Generic ;
9
10
using System . IdentityModel . Tokens . Jwt ;
10
11
using System . Linq ;
11
12
using System . Security . Claims ;
13
+ using System . Security . Cryptography . X509Certificates ;
12
14
using System . Threading . Tasks ;
13
15
14
16
namespace Microsoft . Identity . Web
@@ -22,7 +24,7 @@ public static class WebApiStartupHelpers
22
24
/// <param name="services">Service collection to which to add authentication</param>
23
25
/// <param name="configuration">Configuration</param>
24
26
/// <returns></returns>
25
- public static IServiceCollection AddProtectWebApiWithMicrosoftIdentityPlatformV2 ( this IServiceCollection services , IConfiguration configuration )
27
+ public static IServiceCollection AddProtectWebApiWithMicrosoftIdentityPlatformV2 ( this IServiceCollection services , IConfiguration configuration , X509Certificate2 tokenDecryptionCertificate = null )
26
28
{
27
29
services . AddAuthentication ( AzureADDefaults . JwtBearerAuthenticationScheme )
28
30
. AddAzureADBearer ( options => configuration . Bind ( "AzureAd" , options ) ) ;
@@ -45,6 +47,12 @@ public static IServiceCollection AddProtectWebApiWithMicrosoftIdentityPlatformV2
45
47
// we inject our own multitenant validation logic (which even accepts both V1 and V2 tokens)
46
48
options . TokenValidationParameters . IssuerValidator = AadIssuerValidator . GetIssuerValidator ( options . Authority ) . ValidateAadIssuer ;
47
49
50
+ // If you provide a token decryption certificate, it will be used to decrypt the token
51
+ if ( tokenDecryptionCertificate != null )
52
+ {
53
+ options . TokenValidationParameters . TokenDecryptionKey = new X509SecurityKey ( tokenDecryptionCertificate ) ;
54
+ }
55
+
48
56
// When an access token for our own Web API is validated, we add it to MSAL.NET's cache so that it can
49
57
// be used from the controllers.
50
58
options . Events = new JwtBearerEvents ( ) ;
@@ -66,14 +74,13 @@ public static IServiceCollection AddProtectWebApiWithMicrosoftIdentityPlatformV2
66
74
/// will be kept with the user's claims until the API calls a downstream API. Otherwise the account for the
67
75
/// user is immediately added to the token cache</param>
68
76
/// <returns></returns>
69
- public static IServiceCollection AddProtectedApiCallsWebApis ( this IServiceCollection services , IConfiguration configuration , IEnumerable < string > scopes = null )
77
+ public static IServiceCollection AddProtectedApiCallsWebApis ( this IServiceCollection services , IConfiguration configuration , IEnumerable < string > scopes = null )
70
78
{
71
79
services . AddTokenAcquisition ( ) ;
72
80
services . Configure < JwtBearerOptions > ( AzureADDefaults . JwtBearerAuthenticationScheme , options =>
73
81
{
74
82
// If you don't pre-provide scopes when adding calling AddProtectedApiCallsWebApis, the On behalf of
75
83
// flow will be delayed (lazy construction of MSAL's application
76
-
77
84
options . Events . OnTokenValidated = async context =>
78
85
{
79
86
if ( scopes != null && scopes . Any ( ) )
@@ -87,7 +94,12 @@ public static IServiceCollection AddProtectedApiCallsWebApis(this IServiceCollec
87
94
context . Success ( ) ;
88
95
89
96
// Todo : rather use options.SaveToken?
90
- ( context . Principal . Identity as ClaimsIdentity ) . AddClaim ( new Claim ( "jwt" , ( context . SecurityToken as JwtSecurityToken ) . RawData ) ) ;
97
+ JwtSecurityToken jwtSecurityToken = context . SecurityToken as JwtSecurityToken ;
98
+ if ( jwtSecurityToken != null )
99
+ {
100
+ string rawData = ( jwtSecurityToken . InnerToken != null ) ? jwtSecurityToken . InnerToken . RawData : jwtSecurityToken . RawData ;
101
+ ( context . Principal . Identity as ClaimsIdentity ) . AddClaim ( new Claim ( "jwt" , rawData ) ) ;
102
+ }
91
103
}
92
104
// Adds the token to the cache, and also handles the incremental consent and claim challenges
93
105
await Task . FromResult ( 0 ) ;
0 commit comments