Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect user-provided authority queries and add to options.ExtraQueryParams #2848

Merged
merged 8 commits into from
May 28, 2024
26 changes: 26 additions & 0 deletions src/Microsoft.Identity.Web/AuthorityHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Web;
using Microsoft.AspNetCore.Http;

namespace Microsoft.Identity.Web
Expand Down Expand Up @@ -51,5 +53,29 @@ internal static string EnsureAuthorityIsV2(string authority)
preserveAuthority = true;
return authority;
}

internal static string GetAuthorityWithoutQueryIfNeeded(MicrosoftIdentityOptions options)
{
if (!string.IsNullOrEmpty(options.Authority))
{
int queryIndex = options.Authority.IndexOf('?', StringComparison.Ordinal);
if (queryIndex > -1)
{
options.ExtraQueryParameters ??= new Dictionary<string, string>();
var queryParams = HttpUtility.ParseQueryString(options.Authority[queryIndex..].TrimStart('?'));
for (int i = 0; i < queryParams.Count; i++)
{
var key = queryParams.GetKey(i);
var value = queryParams.Get(i);
if (key != null && value != null)
options.ExtraQueryParameters[key] = value;
}

return options.Authority[..queryIndex];
}
}

return options.Authority ?? string.Empty;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ private static void AddMicrosoftIdentityWebApiImplementation(
// Process OIDC compliant tenants
if (mergedOptions.Authority != null)
{
mergedOptions.Authority = AuthorityHelpers.GetAuthorityWithoutQueryIfNeeded(mergedOptions);
mergedOptions.Authority = AuthorityHelpers.BuildCiamAuthorityIfNeeded(mergedOptions.Authority, out bool preserveAuthority);
mergedOptions.PreserveAuthority = preserveAuthority;
options.Authority = mergedOptions.Authority;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ s.ServiceKey is null &&

if (mergedOptions.Authority != null)
{
mergedOptions.Authority = AuthorityHelpers.GetAuthorityWithoutQueryIfNeeded(mergedOptions);
mergedOptions.Authority = AuthorityHelpers.BuildCiamAuthorityIfNeeded(mergedOptions.Authority, out bool preserveAuthority);
mergedOptions.PreserveAuthority = preserveAuthority;
if (mergedOptions.ExtraQueryParameters != null)
Expand Down
Loading
Loading