A toolset for authorizing access to graph types for GraphQL.NET.
Provides the following packages:
| Package | Downloads | NuGet Latest |
|---|---|---|
| GraphQL.Authorization |
You can get all preview versions from GitHub Packages. Note that GitHub requires authentication to consume the feed. See here.
- Register the authorization classes in your DI container -
IAuthorizationEvaluator,AuthorizationSettings, and theAuthorizationValidationRule. - Provide a custom
UserContextclass that implementsIProvideClaimsPrincipal. - Add policies to the
AuthorizationSettings. - Apply a policy to a GraphType or Field (both implement
IProvideMetadata):- using
AuthorizeWith(string policy)extension method - or with
GraphQLAuthorizeattribute if using Schema + Handler syntax.
- using
- The
AuthorizationValidationRulewill run and verify the policies based on the registered policies. - You can write your own
IAuthorizationRequirement.
-
Fully functional basic Console sample.
-
Fully functional ASP.NET Core sample.
-
GraphType first syntax - use
AuthorizeWithextension method onIGraphTypeorIFieldType.
public class MyType : ObjectGraphType
{
public MyType()
{
this.AuthorizeWith("AdminPolicy");
Field<StringGraphType>("name").AuthorizeWith("SomePolicy");
}
}- Schema first syntax - use
GraphQLAuthorizeattribute on type, method or property.
[GraphQLAuthorize("MyPolicy")]
public class MutationType
{
[GraphQLAuthorize("AnotherPolicy")]
public async Task<string> CreateSomething(MyInput input)
{
return await SomeMethodAsync(input);
}
[GraphQLAuthorize("SuperPolicy")]
public string SomeProperty => Guid.NewGuid().ToString();
}- It is currently not possible to add a policy to Input objects using Schema first approach.