Skip to content

Commit c2249d8

Browse files
authored
Handle tailing slashes in configured Authority and Instance (#278)
* Updated references to 3.1.1 * Handle trailing slash and 'v2.0' in configured Authority value in AddProtectedWebApi method. * Only add api:// to valid audiences if given as Client Id (not beginning with 'api://'. * Handle optional configured ending slash for Instance in `TokenAcquisition`
1 parent fea4d69 commit c2249d8

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

Microsoft.Identity.Web/Microsoft.Identity.Web.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
</ItemGroup>
3535

3636
<ItemGroup Label="Build Tools" Condition="$([MSBuild]::IsOsPlatform('Windows'))">
37-
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0-beta2-18618-05" PrivateAssets="All" />
37+
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
3838
</ItemGroup>
3939

4040

@@ -53,9 +53,9 @@
5353
</PropertyGroup>
5454

5555
<ItemGroup>
56-
<PackageReference Include="Microsoft.AspNetCore.Authentication.AzureAD.UI" Version="3.0.0" />
57-
<PackageReference Include="Microsoft.AspNetCore.Authentication.AzureADB2C.UI" Version="3.0.0" />
58-
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.0.1" />
56+
<PackageReference Include="Microsoft.AspNetCore.Authentication.AzureAD.UI" Version="3.1.1" />
57+
<PackageReference Include="Microsoft.AspNetCore.Authentication.AzureADB2C.UI" Version="3.1.1" />
58+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.1" />
5959
<PackageReference Include="Microsoft.Identity.Client" Version="4.8.1" />
6060
</ItemGroup>
6161
</Project>

Microsoft.Identity.Web/TokenAcquisition.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,9 @@ private IConfidentialClientApplication BuildConfidentialClientApplication()
264264
request.PathBase,
265265
azureAdOptions.CallbackPath ?? string.Empty);
266266

267+
if (!applicationOptions.Instance.EndsWith("/"))
268+
applicationOptions.Instance += "/";
269+
267270
string authority = $"{applicationOptions.Instance}{applicationOptions.TenantId}/";
268271

269272
var app = ConfidentialClientApplicationBuilder

Microsoft.Identity.Web/WebApiServiceCollectionExtensions.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,17 @@ public static IServiceCollection AddProtectedWebApi(
5353
configuration.Bind(configSectionName, options);
5454

5555
// This is an Microsoft identity platform Web API
56-
options.Authority += "/v2.0";
56+
var authority = options.Authority.Trim().TrimEnd('/');
57+
if (!authority.EndsWith("v2.0"))
58+
authority += "/v2.0";
59+
options.Authority = authority;
5760

58-
// The valid audiences are both the Client ID (options.Audience) and api://{ClientID}
59-
options.TokenValidationParameters.ValidAudiences = new string[]
60-
{
61-
options.Audience, $"api://{options.Audience}"
62-
};
61+
// The valid audience could be given as Client Id or as Uri. If it does not start with 'api://', this variant is added to the list of valid audiences.
62+
var validAudiences = new List<string> { options.Audience };
63+
if (!options.Audience.StartsWith("api://", StringComparison.OrdinalIgnoreCase))
64+
validAudiences.Add($"api://{options.Audience}");
65+
66+
options.TokenValidationParameters.ValidAudiences = validAudiences;
6367

6468
// Instead of using the default validation (validating against a single tenant, as we do in line of business apps),
6569
// we inject our own multi-tenant validation logic (which even accepts both v1.0 and v2.0 tokens)
@@ -127,4 +131,4 @@ public static IServiceCollection AddProtectedApiCallsWebApis(
127131
return services;
128132
}
129133
}
130-
}
134+
}

0 commit comments

Comments
 (0)