Change ProtectedResourceMetadata URI properties to strings and build resource strings directly (#1264)

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: halter73 <54385+halter73@users.noreply.github.com>
Co-authored-by: Stephen Halter <halter73@gmail.com>
This commit is contained in:
Copilot 2026-02-12 22:28:43 -05:00 • committed by GitHub
commit dbf91bcb1a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -56,17 +56,29 @@ public partial class McpAuthenticationHandler : AuthenticationHandler<McpAuthent
return false;
}
var deriveResourceUriBuilder = new UriBuilder(Request.Scheme, Request.Host.Host)
// Build the derived resource string directly without trailing slash
var scheme = Request.Scheme;
var host = Request.Host.Host;
var port = Request.Host.Port;
var path = $"{Request.PathBase}{resourceSuffix}".TrimEnd('/');
string derivedResource;
if (port.HasValue && !IsDefaultPort(scheme, port.Value))
{
Path = $"{Request.PathBase}{resourceSuffix}",
};
if (Request.Host.Port is not null)
derivedResource = $"{scheme}://{host}:{port.Value}{path}";
}
else
{
deriveResourceUriBuilder.Port = Request.Host.Port.Value;
derivedResource = $"{scheme}://{host}{path}";
}
return await HandleResourceMetadataRequestAsync(deriveResourceUriBuilder.Uri);
return await HandleResourceMetadataRequestAsync(derivedResource);
}
private static bool IsDefaultPort(string scheme, int port)
{
return (scheme.Equals("http", StringComparison.OrdinalIgnoreCase) && port == 80) ||
(scheme.Equals("https", StringComparison.OrdinalIgnoreCase) && port == 443);
}
/// <summary>
@ -128,9 +140,9 @@ public partial class McpAuthenticationHandler : AuthenticationHandler<McpAuthent
return path.StartsWith('/') ? path : $"/{path}";
}
private async Task<bool> HandleResourceMetadataRequestAsync(Uri? derivedResourceUri = null)
private async Task<bool> HandleResourceMetadataRequestAsync(string? derivedResource = null)
{
var resourceMetadata = Options.ResourceMetadata?.Clone(derivedResourceUri);
var resourceMetadata = Options.ResourceMetadata?.Clone(derivedResource);
if (Options.Events.OnResourceMetadataRequest is not null)
{
@ -165,7 +177,7 @@ public partial class McpAuthenticationHandler : AuthenticationHandler<McpAuthent
throw new InvalidOperationException("ResourceMetadata has not been configured. Please set McpAuthenticationOptions.ResourceMetadata or ensure context.ResourceMetadata is set inside McpAuthenticationOptions.Events.OnResourceMetadataRequest.");
}
resourceMetadata.Resource ??= derivedResourceUri;
resourceMetadata.Resource ??= derivedResource;
if (resourceMetadata.Resource is null)
{