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:
parent
b4c7ded398
commit
dbf91bcb1a
1 changed files with 209 additions and 55 deletions
|
|
@ -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)
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue