Add RequireAuthorization method for default policy (#6916)
This commit is contained in:
parent
dbf82dc8c4
commit
3c7649db88
|
|
@ -4,12 +4,20 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Routing;
|
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.Builder
|
namespace Microsoft.AspNetCore.Builder
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Authorization extension methods for <see cref="IEndpointConventionBuilder"/>.
|
||||||
|
/// </summary>
|
||||||
public static class AuthorizationEndpointConventionBuilderExtensions
|
public static class AuthorizationEndpointConventionBuilderExtensions
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Adds authorization policies with the specified <see cref="IAuthorizeData"/> to the endpoint(s).
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="builder">The endpoint convention builder.</param>
|
||||||
|
/// <param name="authorizeData">A collection of <see cref="IAuthorizeData"/>.</param>
|
||||||
|
/// <returns>The original convention builder parameter.</returns>
|
||||||
public static IEndpointConventionBuilder RequireAuthorization(this IEndpointConventionBuilder builder, params IAuthorizeData[] authorizeData)
|
public static IEndpointConventionBuilder RequireAuthorization(this IEndpointConventionBuilder builder, params IAuthorizeData[] authorizeData)
|
||||||
{
|
{
|
||||||
if (builder == null)
|
if (builder == null)
|
||||||
|
|
@ -32,6 +40,12 @@ namespace Microsoft.AspNetCore.Builder
|
||||||
return builder;
|
return builder;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds authorization policies with the specified names to the endpoint(s).
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="builder">The endpoint convention builder.</param>
|
||||||
|
/// <param name="policyNames">A collection of policy names.</param>
|
||||||
|
/// <returns>The original convention builder parameter.</returns>
|
||||||
public static IEndpointConventionBuilder RequireAuthorization(this IEndpointConventionBuilder builder, params string[] policyNames)
|
public static IEndpointConventionBuilder RequireAuthorization(this IEndpointConventionBuilder builder, params string[] policyNames)
|
||||||
{
|
{
|
||||||
if (builder == null)
|
if (builder == null)
|
||||||
|
|
@ -46,5 +60,20 @@ namespace Microsoft.AspNetCore.Builder
|
||||||
|
|
||||||
return builder.RequireAuthorization(policyNames.Select(n => new AuthorizeAttribute(n)).ToArray());
|
return builder.RequireAuthorization(policyNames.Select(n => new AuthorizeAttribute(n)).ToArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds the default authorization policy to the endpoint(s).
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="builder">The endpoint convention builder.</param>
|
||||||
|
/// <returns>The original convention builder parameter.</returns>
|
||||||
|
public static IEndpointConventionBuilder RequireAuthorization(this IEndpointConventionBuilder builder)
|
||||||
|
{
|
||||||
|
if (builder == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(builder));
|
||||||
|
}
|
||||||
|
|
||||||
|
return builder.RequireAuthorization(new AuthorizeAttribute());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -47,7 +47,27 @@ namespace Microsoft.AspNetCore.Authorization.Test
|
||||||
var endpointModel = new RouteEndpointBuilder((context) => Task.CompletedTask, RoutePatternFactory.Parse("/"), 0);
|
var endpointModel = new RouteEndpointBuilder((context) => Task.CompletedTask, RoutePatternFactory.Parse("/"), 0);
|
||||||
convention(endpointModel);
|
convention(endpointModel);
|
||||||
|
|
||||||
Assert.Equal("policy", Assert.IsAssignableFrom<IAuthorizeData>(Assert.Single(endpointModel.Metadata)).Policy);
|
var authMetadata = Assert.IsAssignableFrom<IAuthorizeData>(Assert.Single(endpointModel.Metadata));
|
||||||
|
Assert.Equal("policy", authMetadata.Policy);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void RequireAuthorization_Default()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var builder = new TestEndpointConventionBuilder();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
builder.RequireAuthorization();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
var convention = Assert.Single(builder.Conventions);
|
||||||
|
|
||||||
|
var endpointModel = new RouteEndpointBuilder((context) => Task.CompletedTask, RoutePatternFactory.Parse("/"), 0);
|
||||||
|
convention(endpointModel);
|
||||||
|
|
||||||
|
var authMetadata = Assert.IsAssignableFrom<IAuthorizeData>(Assert.Single(endpointModel.Metadata));
|
||||||
|
Assert.Null(authMetadata.Policy);
|
||||||
}
|
}
|
||||||
|
|
||||||
private class TestEndpointConventionBuilder : IEndpointConventionBuilder
|
private class TestEndpointConventionBuilder : IEndpointConventionBuilder
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue