Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benmccallum/fbfcf8758fc0bd80a723b09fae18f6f4 to your computer and use it in GitHub Desktop.
Save benmccallum/fbfcf8758fc0bd80a723b09fae18f6f4 to your computer and use it in GitHub Desktop.
HCv13 AddAuthorization extension method

Before

services
    .AddAuthorization(options =>
    {
        options.AddPolicy("person", b => b.RequireAssertion(ctx => false));
    })

services
    .AddGraphQLServer()
    .AddAuthorization();

After

services
    .AddGraphQLServer()
    .AddAuthorization(options =>
    {
        options.AddPolicy("person", b => b.RequireAssertion(ctx => false));
    })
using System;
using HotChocolate;
using HotChocolate.AspNetCore.Authorization;
using HotChocolate.Execution.Configuration;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.DependencyInjection.Extensions;
namespace Microsoft.Extensions.DependencyInjection
{
/// <summary>
/// Provides extension methods for the GraphQL builder.
/// </summary>
public static class HotChocolateAuthorizeRequestExecutorBuilder
{
/// <summary>
/// Adds ASP.NET Core authorization support to the schema.
/// </summary>
/// <param name="builder">
/// The <see cref="IRequestExecutorBuilder"/>.
/// </param>
/// <param name="configure">
/// An action delegate to configure the provided <see cref="AuthorizationOptions"/>.
/// </param>
/// <returns>
/// Returns the <see cref="IRequestExecutorBuilder"/> for chaining in more configurations.
/// </returns>
public static IRequestExecutorBuilder AddAuthorization(
this IRequestExecutorBuilder builder,
Action<AuthorizationOptions> configure)
{
builder.ConfigureSchema(sb => sb.AddAuthorizeDirectiveType());
builder.Services.TryAddSingleton<
HotChocolate.AspNetCore.Authorization.IAuthorizationHandler,
DefaultAuthorizationHandler>();
builder.Services.AddAuthorization(configure);
return builder;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment