Skip to content

Commit b14dc07

Browse files
author
Kalyan Krishna
committed
Updated "System.IdentityModel.Tokens.Jwt" to "5.2.4"
1 parent e3ada1c commit b14dc07

File tree

4 files changed

+47
-31
lines changed

4 files changed

+47
-31
lines changed

TodoListService-ManualJwt/Global.asax.cs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,20 @@
2424
using System.Web.Routing;
2525

2626
// The following using statements were added for this sample.
27-
using System.Net.Http;
28-
using System.IdentityModel.Tokens;
29-
using System.Threading.Tasks;
27+
using System.Net.Http;using System.Threading.Tasks;
3028
using System.Threading;
3129
using System.Net;
3230
using System.IdentityModel.Selectors;
3331
using System.Security.Claims;
3432
using System.Net.Http.Headers;
35-
using System.IdentityModel.Metadata;
33+
using Microsoft.IdentityModel.Tokens;
3634
using System.ServiceModel.Security;
3735
using System.Xml;
38-
using System.Security.Cryptography.X509Certificates;
36+
using System.IdentityModel.Tokens.Jwt;
3937
using System.Globalization;
4038
using System.Configuration;
4139
using Microsoft.IdentityModel.Protocols;
40+
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
4241

4342
namespace TodoListService_ManualJwt
4443
{
@@ -70,7 +69,7 @@ internal class TokenValidationHandler : DelegatingHandler
7069
string authority = String.Format(CultureInfo.InvariantCulture, aadInstance, tenant);
7170

7271
static string _issuer = string.Empty;
73-
static List<SecurityToken> _signingTokens = null;
72+
static ICollection<SecurityKey> _signingKeys = null;
7473
static DateTime _stsMetadataRetrievalTime = DateTime.MinValue;
7574
static string scopeClaimType = "http://schemas.microsoft.com/identity/claims/scope";
7675

@@ -89,32 +88,32 @@ protected async override Task<HttpResponseMessage> SendAsync(HttpRequestMessage
8988

9089
if (jwtToken == null)
9190
{
92-
HttpResponseMessage response = BuildResponseErrorMessage(HttpStatusCode.Unauthorized);
91+
HttpResponseMessage response = this.BuildResponseErrorMessage(HttpStatusCode.Unauthorized);
9392
return response;
9493
}
9594

9695
string issuer;
97-
List<SecurityToken> signingTokens;
96+
ICollection<SecurityKey> signingTokens;
9897

9998
try
10099
{
101100
// The issuer and signingTokens are cached for 24 hours. They are updated if any of the conditions in the if condition is true.
102101
if (DateTime.UtcNow.Subtract(_stsMetadataRetrievalTime).TotalHours > 24
103102
|| string.IsNullOrEmpty(_issuer)
104-
|| _signingTokens == null)
103+
|| _signingKeys == null)
105104
{
106105
// Get tenant information that's used to validate incoming jwt tokens
107-
string stsDiscoveryEndpoint = string.Format("{0}/.well-known/openid-configuration", authority);
108-
ConfigurationManager<OpenIdConnectConfiguration> configManager = new ConfigurationManager<OpenIdConnectConfiguration>(stsDiscoveryEndpoint);
109-
OpenIdConnectConfiguration config = await configManager.GetConfigurationAsync();
106+
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);
110109
_issuer = config.Issuer;
111-
_signingTokens = config.SigningTokens.ToList();
110+
_signingKeys = config.SigningKeys;
112111

113112
_stsMetadataRetrievalTime = DateTime.UtcNow;
114113
}
115114

116115
issuer = _issuer;
117-
signingTokens = _signingTokens;
116+
signingTokens = _signingKeys;
118117
}
119118
catch (Exception)
120119
{
@@ -130,8 +129,7 @@ protected async override Task<HttpResponseMessage> SendAsync(HttpRequestMessage
130129

131130
// Supports both the Azure AD V1 and V2 endpoint
132131
ValidIssuers = new [] { issuer, $"{issuer}/v2.0" },
133-
IssuerSigningTokens = signingTokens,
134-
CertificateValidator = X509CertificateValidator.None // Certificate validation does not make sense since AAD's metadata document is signed with a self-signed certificate.
132+
IssuerSigningKeys = signingTokens
135133
};
136134

137135
try
@@ -152,15 +150,15 @@ protected async override Task<HttpResponseMessage> SendAsync(HttpRequestMessage
152150
// If the token is scoped, verify that required permission is set in the scope claim.
153151
if (ClaimsPrincipal.Current.FindFirst(scopeClaimType) != null && ClaimsPrincipal.Current.FindFirst(scopeClaimType).Value != "user_impersonation")
154152
{
155-
HttpResponseMessage response = BuildResponseErrorMessage(HttpStatusCode.Forbidden);
153+
HttpResponseMessage response = this.BuildResponseErrorMessage(HttpStatusCode.Forbidden);
156154
return response;
157155
}
158156

159157
return await base.SendAsync(request, cancellationToken);
160158
}
161159
catch (SecurityTokenValidationException)
162160
{
163-
HttpResponseMessage response = BuildResponseErrorMessage(HttpStatusCode.Unauthorized);
161+
HttpResponseMessage response = this.BuildResponseErrorMessage(HttpStatusCode.Unauthorized);
164162
return response;
165163
}
166164
catch (Exception)

TodoListService-ManualJwt/TodoListService-ManualJwt.csproj

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,31 @@
4646
<HintPath>..\packages\Antlr.3.5.0.2\lib\Antlr3.Runtime.dll</HintPath>
4747
</Reference>
4848
<Reference Include="Microsoft.CSharp" />
49-
<Reference Include="Microsoft.IdentityModel.Protocol.Extensions, Version=1.0.40306.1554, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
50-
<HintPath>..\packages\Microsoft.IdentityModel.Protocol.Extensions.1.0.4.403061554\lib\net45\Microsoft.IdentityModel.Protocol.Extensions.dll</HintPath>
49+
<Reference Include="Microsoft.IdentityModel.JsonWebTokens, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
50+
<HintPath>..\packages\Microsoft.IdentityModel.JsonWebTokens.5.2.4\lib\net45\Microsoft.IdentityModel.JsonWebTokens.dll</HintPath>
5151
</Reference>
52-
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
53-
<SpecificVersion>False</SpecificVersion>
54-
<HintPath>..\packages\Newtonsoft.Json.6.0.5\lib\net45\Newtonsoft.Json.dll</HintPath>
52+
<Reference Include="Microsoft.IdentityModel.Logging, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
53+
<HintPath>..\packages\Microsoft.IdentityModel.Logging.5.2.4\lib\net45\Microsoft.IdentityModel.Logging.dll</HintPath>
54+
</Reference>
55+
<Reference Include="Microsoft.IdentityModel.Protocols, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
56+
<HintPath>..\packages\Microsoft.IdentityModel.Protocols.5.2.4\lib\net45\Microsoft.IdentityModel.Protocols.dll</HintPath>
57+
</Reference>
58+
<Reference Include="Microsoft.IdentityModel.Protocols.OpenIdConnect, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
59+
<HintPath>..\packages\Microsoft.IdentityModel.Protocols.OpenIdConnect.5.2.4\lib\net45\Microsoft.IdentityModel.Protocols.OpenIdConnect.dll</HintPath>
60+
</Reference>
61+
<Reference Include="Microsoft.IdentityModel.Tokens, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
62+
<HintPath>..\packages\Microsoft.IdentityModel.Tokens.5.2.4\lib\net45\Microsoft.IdentityModel.Tokens.dll</HintPath>
63+
</Reference>
64+
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
65+
<HintPath>..\packages\Newtonsoft.Json.10.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
5566
</Reference>
5667
<Reference Include="System" />
5768
<Reference Include="System.Data" />
5869
<Reference Include="System.Data.Entity" />
5970
<Reference Include="System.Drawing" />
6071
<Reference Include="System.IdentityModel" />
61-
<Reference Include="System.IdentityModel.Tokens.Jwt, Version=4.0.40306.1554, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
62-
<HintPath>..\packages\System.IdentityModel.Tokens.Jwt.4.0.4.403061554\lib\net45\System.IdentityModel.Tokens.Jwt.dll</HintPath>
72+
<Reference Include="System.IdentityModel.Tokens.Jwt, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
73+
<HintPath>..\packages\System.IdentityModel.Tokens.Jwt.5.2.4\lib\net45\System.IdentityModel.Tokens.Jwt.dll</HintPath>
6374
</Reference>
6475
<Reference Include="System.Net.Http.Formatting, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
6576
<HintPath>..\packages\Microsoft.AspNet.WebApi.Client.5.2.4\lib\net45\System.Net.Http.Formatting.dll</HintPath>

TodoListService-ManualJwt/Web.config

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
<compilation debug="true" targetFramework="4.5" />
1919
<httpRuntime targetFramework="4.5" />
2020
</system.web>
21-
2221
<runtime>
2322
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
2423
<dependentAssembly>
@@ -35,7 +34,7 @@
3534
</dependentAssembly>
3635
<dependentAssembly>
3736
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
38-
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
37+
<bindingRedirect oldVersion="0.0.0.0-10.0.0.0" newVersion="10.0.0.0" />
3938
</dependentAssembly>
4039
<dependentAssembly>
4140
<assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" />
@@ -57,6 +56,10 @@
5756
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
5857
<bindingRedirect oldVersion="1.0.0.0-5.2.4.0" newVersion="5.2.4.0" />
5958
</dependentAssembly>
59+
<dependentAssembly>
60+
<assemblyIdentity name="System.IdentityModel.Tokens.Jwt" publicKeyToken="31bf3856ad364e35" culture="neutral" />
61+
<bindingRedirect oldVersion="0.0.0.0-5.2.4.0" newVersion="5.2.4.0" />
62+
</dependentAssembly>
6063
</assemblyBinding>
6164
</runtime>
6265
<system.webServer>
@@ -67,4 +70,4 @@
6770
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
6871
</handlers>
6972
</system.webServer>
70-
</configuration>
73+
</configuration>

TodoListService-ManualJwt/packages.config

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,15 @@
1212
<package id="Microsoft.AspNet.WebApi.HelpPage" version="5.2.4" targetFramework="net45" />
1313
<package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.4" targetFramework="net45" />
1414
<package id="Microsoft.AspNet.WebPages" version="3.2.4" targetFramework="net45" />
15-
<package id="Microsoft.IdentityModel.Protocol.Extensions" version="1.0.4.403061554" targetFramework="net45" />
15+
<package id="Microsoft.IdentityModel.JsonWebTokens" version="5.2.4" targetFramework="net45" />
16+
<package id="Microsoft.IdentityModel.Logging" version="5.2.4" targetFramework="net45" />
17+
<package id="Microsoft.IdentityModel.Protocols" version="5.2.4" targetFramework="net45" />
18+
<package id="Microsoft.IdentityModel.Protocols.OpenIdConnect" version="5.2.4" targetFramework="net45" />
19+
<package id="Microsoft.IdentityModel.Tokens" version="5.2.4" targetFramework="net45" />
1620
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net45" />
1721
<package id="Modernizr" version="2.8.3" targetFramework="net45" />
18-
<package id="Newtonsoft.Json" version="6.0.5" targetFramework="net45" />
22+
<package id="Newtonsoft.Json" version="10.0.1" targetFramework="net45" />
1923
<package id="Respond" version="1.4.2" targetFramework="net45" />
20-
<package id="System.IdentityModel.Tokens.Jwt" version="4.0.4.403061554" targetFramework="net45" />
24+
<package id="System.IdentityModel.Tokens.Jwt" version="5.2.4" targetFramework="net45" />
2125
<package id="WebGrease" version="1.6.0" targetFramework="net45" />
2226
</packages>

0 commit comments

Comments
 (0)