-
I have a bit of a strange conundrum here. We are running .NET Core 6 and just updated to GraphQL 7 (from 5) - we are using DI and MVC and have been running GraphQL.NET since version 3. Everything is working fine, except authorization seems to be either all or nothing. All would be acceptable except the Introspection endpoints need to allow anonymous so that our front end Apollo implementation can digest it without authenticating. So, if I have the It seems like I have something misconfigured somewhere, or I have missed something, but I am not certain what it could be. Our authentication stuff is pretty vanilla .NET role-base auth. Any direction you could provide would be helpful - happy to provide code samples if needed. Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Endpoint authorization, as described here in the readme, is all-or-nothing and applies authorization policies prior to allowing any connection to the GraphQL endpoint, whether for introspection requests or otherwise. Field/type authorization, as described here in the readme, will allow introspection requests, as introspection fields implicitly have // code-first sample
public class QueryGraphType : ObjectGraphType
{
public QueryGraphType()
{
this.AuthorizeWithRoles("Administrators");
// fields here
Field<string>("Hello").Resolve(_ => "World");
}
}
// type-first sample
[Authorize(Roles = "Administrators")]
public class Query
{
// fields here
public static string Hello => "World";
}
//ditto for mutation and subscription types If you those settings do not seem to work, be sure that you have called |
Beta Was this translation helpful? Give feedback.
-
Be sure not to call any authorization extension methods within a field resolver. At that point the field is already being executed, and has passed validation. Call // incorrect
Field<string>("Hello")
.Resolve(_ =>
{
this.AuthorizeWithRoles("Administrators");
return "World";
});
// correct
Field<string>("Hello")
.Resolve(_ => "World")
.AuthorizeWithRoles("Administrators");
// it seems the .Authorize() extension is missing from the field builder, so for now, do this for .Authorize():
Field<string>("Hello")
.Resolve(_ => "World")
.FieldType.Authorize(); See graphql-dotnet/graphql-dotnet#3324 for fix to add |
Beta Was this translation helpful? Give feedback.
Endpoint authorization, as described here in the readme, is all-or-nothing and applies authorization policies prior to allowing any connection to the GraphQL endpoint, whether for introspection requests or otherwise.
Field/type authorization, as described here in the readme, will allow introspection requests, as introspection fields implicitly have
[AllowAnonymous]
set. If you need a global authorization policy set while allowing introspection requests, simply add the proper policy to the query, mutation and subscription types.