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 (which implement
IProvideMetadata) usingAuthorizeWith(string policy). - Make sure the
AuthorizationValidationRuleis registered with your Schema (depending on your server implementation, you may only need to register it in your DI container) - The
AuthorizationValidationRulewill run and verify the policies based on the registered policies. - You can write your own
IAuthorizationRequirement. - Use
GraphQLAuthorizeattribute if using Schema First syntax.
-
Fully functional basic Console sample.
-
Fully functional ASP.NET Core sample.
-
GraphType first syntax - use
AuthorizeWith.
public class MyType : ObjectGraphType
{
public MyType()
{
this.AuthorizeWith("AdminPolicy");
Field<StringGraphType>("name").AuthorizeWith("SomePolicy");
}
}- Schema first syntax - use
GraphQLAuthorizeattribute.
[GraphQLAuthorize(Policy = "MyPolicy")]
public class MutationType
{
[GraphQLAuthorize(Policy = "AnotherPolicy")]
public async Task<string> CreateSomething(MyInput input)
{
return Guid.NewGuid().ToString();
}
}- It is currently not possible to add a policy to Input objects using Schema first approach.