Skip to content

Commit 89bd338

Browse files
authored
Address API review feedback for what was IApiEndpointMetadata (#63283)
1 parent dcec533 commit 89bd338

File tree

114 files changed

+603
-411
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

114 files changed

+603
-411
lines changed

src/Http/Http.Abstractions/src/Metadata/ApiEndpointMetadata.cs

Lines changed: 0 additions & 20 deletions
This file was deleted.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace Microsoft.AspNetCore.Http.Metadata;
5+
6+
/// <summary>
7+
/// Metadata that indicates the endpoint should disable cookie-based authentication redirects.
8+
/// When present, authentication handlers should prefer returning status codes over browser redirects.
9+
/// </summary>
10+
internal sealed class DisableCookieRedirectMetadata : IDisableCookieRedirectMetadata
11+
{
12+
/// <summary>
13+
/// Singleton instance of <see cref="DisableCookieRedirectMetadata"/>.
14+
/// </summary>
15+
public static readonly DisableCookieRedirectMetadata Instance = new();
16+
17+
private DisableCookieRedirectMetadata()
18+
{
19+
}
20+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace Microsoft.AspNetCore.Http.Metadata;
5+
6+
/// <summary>
7+
/// Metadata that indicates the endpoint should allow cookie-based authentication redirects.
8+
/// This is normally the default behavior, but it exists to override <see cref="IDisableCookieRedirectMetadata"/> no matter the order.
9+
/// When present, the cookie authentication handler will prefer browser login or access denied redirects over 401 and 403 status codes.
10+
/// </summary>
11+
public interface IAllowCookieRedirectMetadata
12+
{
13+
}

src/Http/Http.Abstractions/src/Metadata/IApiEndpointMetadata.cs

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace Microsoft.AspNetCore.Http.Metadata;
5+
6+
/// <summary>
7+
/// Metadata that indicates the endpoint should disable cookie-based authentication redirects
8+
/// typically because it is intended for API clients rather than direct browser navigation.
9+
///
10+
/// <see cref="IAllowCookieRedirectMetadata"/> overrides this no matter the order.
11+
///
12+
/// When present and not overridden, the cookie authentication handler will prefer using
13+
/// 401 and 403 status codes over redirecting to the login or access denied paths.
14+
/// </summary>
15+
public interface IDisableCookieRedirectMetadata
16+
{
17+
}

src/Http/Http.Abstractions/src/PublicAPI.Unshipped.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#nullable enable
2-
Microsoft.AspNetCore.Http.Metadata.IApiEndpointMetadata
2+
Microsoft.AspNetCore.Http.Metadata.IAllowCookieRedirectMetadata
3+
Microsoft.AspNetCore.Http.Metadata.IDisableCookieRedirectMetadata
34
Microsoft.AspNetCore.Http.Metadata.IDisableValidationMetadata
45
Microsoft.AspNetCore.Http.ProducesResponseTypeMetadata.Description.get -> string?
56
Microsoft.AspNetCore.Http.ProducesResponseTypeMetadata.Description.set -> void

src/Http/Http.Extensions/gen/Microsoft.AspNetCore.Http.RequestDelegateGenerator/RequestDelegateGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
253253

254254
if (hasJsonBody || hasResponseMetadata)
255255
{
256-
codeWriter.WriteLine(RequestDelegateGeneratorSources.ApiEndpointMetadataClass);
256+
codeWriter.WriteLine(RequestDelegateGeneratorSources.DisableCookieRedirectMetadataClass);
257257
}
258258

259259
if (hasFormBody || hasJsonBody || hasResponseMetadata)

src/Http/Http.Extensions/gen/Microsoft.AspNetCore.Http.RequestDelegateGenerator/RequestDelegateGeneratorSources.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -493,18 +493,18 @@ public AntiforgeryMetadata(bool requiresValidation)
493493
}
494494
""";
495495

496-
public static string ApiEndpointMetadataClass = """
497-
file sealed class ApiEndpointMetadata : IApiEndpointMetadata
496+
public static string DisableCookieRedirectMetadataClass = """
497+
file sealed class DisableCookieRedirectMetadata : IDisableCookieRedirectMetadata
498498
{
499-
public static readonly ApiEndpointMetadata Instance = new();
499+
public static readonly DisableCookieRedirectMetadata Instance = new();
500500
501-
private ApiEndpointMetadata()
501+
private DisableCookieRedirectMetadata()
502502
{
503503
}
504504
505-
public static void AddApiEndpointMetadataIfMissing(EndpointBuilder builder)
505+
public static void AddMetadataIfMissing(EndpointBuilder builder)
506506
{
507-
if (!builder.Metadata.Any(m => m is IApiEndpointMetadata))
507+
if (!builder.Metadata.Any(m => m is IDisableCookieRedirectMetadata))
508508
{
509509
builder.Metadata.Add(Instance);
510510
}

src/Http/Http.Extensions/gen/Microsoft.AspNetCore.Http.RequestDelegateGenerator/StaticRouteHandlerModel/StaticRouteHandlerModel.Emitter.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ private static void EmitBuiltinResponseTypeMetadata(this Endpoint endpoint, Code
218218
else if (response.ResponseType is { } responseType)
219219
{
220220
codeWriter.WriteLine($$"""options.EndpointBuilder.Metadata.Add(new ProducesResponseTypeMetadata(statusCode: StatusCodes.Status200OK, type: typeof({{responseType.ToDisplayString(EmitterConstants.DisplayFormatWithoutNullability)}}), contentTypes: GeneratedMetadataConstants.JsonContentType));""");
221-
codeWriter.WriteLine("ApiEndpointMetadata.AddApiEndpointMetadataIfMissing(options.EndpointBuilder);");
221+
codeWriter.WriteLine("DisableCookieRedirectMetadata.AddMetadataIfMissing(options.EndpointBuilder);");
222222
}
223223
}
224224

@@ -336,15 +336,15 @@ public static void EmitJsonAcceptsMetadata(this Endpoint endpoint, CodeWriter co
336336
codeWriter.WriteLine("if (!serviceProviderIsService.IsService(type))");
337337
codeWriter.StartBlock();
338338
codeWriter.WriteLine("options.EndpointBuilder.Metadata.Add(new AcceptsMetadata(type: type, isOptional: isOptional, contentTypes: GeneratedMetadataConstants.JsonContentType));");
339-
codeWriter.WriteLine("options.EndpointBuilder.Metadata.Add(ApiEndpointMetadata.Instance);");
339+
codeWriter.WriteLine("options.EndpointBuilder.Metadata.Add(DisableCookieRedirectMetadata.Instance);");
340340
codeWriter.WriteLine("break;");
341341
codeWriter.EndBlock();
342342
codeWriter.EndBlock();
343343
}
344344
else
345345
{
346346
codeWriter.WriteLine("options.EndpointBuilder.Metadata.Add(new AcceptsMetadata(contentTypes: GeneratedMetadataConstants.JsonContentType));");
347-
codeWriter.WriteLine("options.EndpointBuilder.Metadata.Add(ApiEndpointMetadata.Instance);");
347+
codeWriter.WriteLine("options.EndpointBuilder.Metadata.Add(DisableCookieRedirectMetadata.Instance);");
348348
}
349349
}
350350

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using Microsoft.AspNetCore.Http.Metadata;
5+
6+
namespace Microsoft.AspNetCore.Http;
7+
8+
/// <summary>
9+
/// Specifies that cookie-based authentication redirects are allowed for an endpoint.
10+
/// This is normally the default behavior, but it exists to override <see cref="IDisableCookieRedirectMetadata"/> no matter the order.
11+
/// When present, the cookie authentication handler will prefer browser login or access denied redirects over 401 and 403 status codes.
12+
/// </summary>
13+
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
14+
public sealed class AllowCookieRedirectAttribute : Attribute, IAllowCookieRedirectMetadata
15+
{
16+
}

0 commit comments

Comments
 (0)