1414// limitations under the License.
1515//----------------------------------------------------------------------------------------------
1616
17+ using Microsoft . IdentityModel . Protocols ;
18+ using Microsoft . IdentityModel . Protocols . OpenIdConnect ;
19+ using Microsoft . IdentityModel . Tokens ;
1720using System ;
1821using System . Collections . Generic ;
19- using System . Linq ;
22+ using System . Configuration ;
23+ using System . Globalization ;
24+ using System . IdentityModel . Tokens . Jwt ;
25+ using System . Net ;
26+ 27+ // The following using statements were added for this sample.
28+ using System . Net . Http ;
29+ using System . Net . Http . Headers ;
30+ using System . Security . Claims ;
31+ using System . Threading ;
32+ using System . Threading . Tasks ;
2033using System . Web ;
2134using System . Web . Http ;
2235using System . Web . Mvc ;
2336using System . Web . Optimization ;
2437using System . Web . Routing ;
2538
26- // The following using statements were added for this sample.
27- using System . Net . Http ; using System . Threading . Tasks ;
28- using System . Threading ;
29- using System . Net ;
30- using System . IdentityModel . Selectors ;
31- using System . Security . Claims ;
32- using System . Net . Http . Headers ;
33- using Microsoft . IdentityModel . Tokens ;
34- using System . ServiceModel . Security ;
35- using System . Xml ;
36- using System . IdentityModel . Tokens . Jwt ;
37- using System . Globalization ;
38- using System . Configuration ;
39- using Microsoft . IdentityModel . Protocols ;
40- using Microsoft . IdentityModel . Protocols . OpenIdConnect ;
41- 4239namespace TodoListService_ManualJwt
4340{
4441 public class WebApiApplication : System . Web . HttpApplication
@@ -62,17 +59,18 @@ internal class TokenValidationHandler : DelegatingHandler
6259 // The Authority is the sign-in URL of the tenant.
6360 // The Audience is the value the service expects to see in tokens that are addressed to it.
6461 //
65- static string aadInstance = ConfigurationManager . AppSettings [ "ida:AADInstance" ] ;
66- static string tenant = ConfigurationManager . AppSettings [ "ida:Tenant" ] ;
67- static string audience = ConfigurationManager . AppSettings [ "ida:Audience" ] ;
68- static string clientId = ConfigurationManager . AppSettings [ "ida:ClientId" ] ;
69- string authority = String . Format ( CultureInfo . InvariantCulture , aadInstance , tenant ) ;
70- 71- static string _issuer = string . Empty ;
72- static ICollection < SecurityKey > _signingKeys = null ;
73- static DateTime _stsMetadataRetrievalTime = DateTime . MinValue ;
74- static string scopeClaimType = "http://schemas.microsoft.com/identity/claims/scope" ;
75- 62+ private static string aadInstance = ConfigurationManager . AppSettings [ "ida:AADInstance" ] ;
63+ 64+ private static string tenant = ConfigurationManager . AppSettings [ "ida:Tenant" ] ;
65+ private static string audience = ConfigurationManager . AppSettings [ "ida:Audience" ] ;
66+ private static string clientId = ConfigurationManager . AppSettings [ "ida:ClientId" ] ;
67+ private string authority = String . Format ( CultureInfo . InvariantCulture , aadInstance , tenant ) ;
68+ 69+ private static string _issuer = string . Empty ;
70+ private static ICollection < SecurityKey > _signingKeys = null ;
71+ private static DateTime _stsMetadataRetrievalTime = DateTime . MinValue ;
72+ private static string scopeClaimType = "http://schemas.microsoft.com/identity/claims/scope" ;
73+ 7674 //
7775 // SendAsync checks that incoming requests have a valid access token, and sets the current user identity using that access token.
7876 //
@@ -93,27 +91,27 @@ protected async override Task<HttpResponseMessage> SendAsync(HttpRequestMessage
9391 }
9492
9593 string issuer ;
96- ICollection < SecurityKey > signingTokens ;
94+ ICollection < SecurityKey > signingKeys ;
9795
9896 try
9997 {
100- // The issuer and signingTokens are cached for 24 hours. They are updated if any of the conditions in the if condition is true.
98+ // The issuer and signingKeys are cached for 24 hours. They are updated if any of the conditions in the if condition is true.
10199 if ( DateTime . UtcNow . Subtract ( _stsMetadataRetrievalTime ) . TotalHours > 24
102100 || string . IsNullOrEmpty ( _issuer )
103101 || _signingKeys == null )
104102 {
105103 // Get tenant information that's used to validate incoming jwt tokens
106104 string stsDiscoveryEndpoint = $ "{ this . authority } /.well-known/openid-configuration";
107- Microsoft . IdentityModel . Protocols . ConfigurationManager < Microsoft . IdentityModel . Protocols . OpenIdConnect . OpenIdConnectConfiguration > configManager = new ConfigurationManager < OpenIdConnectConfiguration > ( stsDiscoveryEndpoint , new OpenIdConnectConfigurationRetriever ( ) ) ;
108- Microsoft . IdentityModel . Protocols . OpenIdConnect . OpenIdConnectConfiguration config = await configManager . GetConfigurationAsync ( cancellationToken ) ;
105+ var configManager = new ConfigurationManager < OpenIdConnectConfiguration > ( stsDiscoveryEndpoint , new OpenIdConnectConfigurationRetriever ( ) ) ;
106+ var config = await configManager . GetConfigurationAsync ( cancellationToken ) ;
109107 _issuer = config . Issuer ;
110108 _signingKeys = config . SigningKeys ;
111-
109+ 112110 _stsMetadataRetrievalTime = DateTime . UtcNow ;
113111 }
114112
115113 issuer = _issuer ;
116- signingTokens = _signingKeys ;
114+ signingKeys = _signingKeys ;
117115 }
118116 catch ( Exception )
119117 {
@@ -128,8 +126,8 @@ protected async override Task<HttpResponseMessage> SendAsync(HttpRequestMessage
128126 ValidAudiences = new [ ] { audience , clientId } ,
129127
130128 // Supports both the Azure AD V1 and V2 endpoint
131- ValidIssuers = new [ ] { issuer , $ "{ issuer } /v2.0" } ,
132- IssuerSigningKeys = signingTokens
129+ ValidIssuers = new [ ] { issuer , $ "{ issuer } /v2.0" } ,
130+ IssuerSigningKeys = signingKeys
133131 } ;
134132
135133 try
@@ -174,11 +172,11 @@ private HttpResponseMessage BuildResponseErrorMessage(HttpStatusCode statusCode)
174172 //
175173 // The Scheme should be "Bearer", authorization_uri should point to the tenant url and resource_id should point to the audience.
176174 //
177- AuthenticationHeaderValue authenticateHeader = new AuthenticationHeaderValue ( "Bearer" , "authorization_uri=\" " + authority + "\" " + "," + "resource_id=" + audience ) ;
175+ AuthenticationHeaderValue authenticateHeader = new AuthenticationHeaderValue ( "Bearer" , "authorization_uri=\" " + this . authority + "\" " + "," + "resource_id=" + audience ) ;
178176
179177 response . Headers . WwwAuthenticate . Add ( authenticateHeader ) ;
180178
181179 return response ;
182180 }
183181 }
184- }
182+ }
0 commit comments