Authorization Support (Using ASP.NET Core Native AuthN/AuthZ Integration) (#377)

This commit is contained in:
Den Delimarsky 🌺 2025-07-03 14:39:28 -07:00 • committed by GitHub
commit cdf88e7f5d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -0,0 +1,49 @@
using Microsoft.AspNetCore.Authentication;
using ModelContextProtocol.Authentication;
namespace ModelContextProtocol.AspNetCore.Authentication;
/// <summary>
/// Options for the MCP authentication handler.
/// </summary>
public class McpAuthenticationOptions : AuthenticationSchemeOptions
{
private static readonly Uri DefaultResourceMetadataUri = new("/.well-known/oauth-protected-resource", UriKind.Relative);
/// <summary>
/// Initializes a new instance of the <see cref="McpAuthenticationOptions"/> class.
/// </summary>
public McpAuthenticationOptions()
{
// "Bearer" is JwtBearerDefaults.AuthenticationScheme, but we don't have a reference to the JwtBearer package here.
ForwardAuthenticate = "Bearer";
ResourceMetadataUri = DefaultResourceMetadataUri;
Events = new McpAuthenticationEvents();
}
/// <summary>
/// Gets or sets the events used to handle authentication events.
/// </summary>
public new McpAuthenticationEvents Events
{
get { return (McpAuthenticationEvents)base.Events!; }
set { base.Events = value; }
}
/// <summary>
/// The URI to the resource metadata document.
/// </summary>
/// <remarks>
/// This URI will be included in the WWW-Authenticate header when a 401 response is returned.
/// </remarks>
public Uri ResourceMetadataUri { get; set; }
/// <summary>
/// Gets or sets the protected resource metadata.
/// </summary>
/// <remarks>
/// This contains the OAuth metadata for the protected resource, including authorization servers,
/// supported scopes, and other information needed for clients to authenticate.
/// </remarks>
public ProtectedResourceMetadata? ResourceMetadata { get; set; }
}