GraphQL实体框架核心身份验证被忽略

时间:2019-06-29 12:10:14

标签: c# graphql

我目前正在使用.NET Core,Azure AD B2C和Entity Framework Core开发GraphQL API。我遵循了我可以找到的所有教程,但是对我不起作用(它忽略了我的政策)。这可能是因为我正在使用GraphQL.EntityFramework。尽管它们只是GraphQL之上的额外一层。我找不到我做错了什么?

我在这样的字段上添加了授权:

在我的query.cs中:

AddSingleField(
            name: "BasicField",
            resolve: context => repo.GetAll().AsNoTracking()).AuthorizeWith("Authorized");

在我的创业公司:

services.AddAuthentication(option =>
                {
                    option.DefaultAuthenticateScheme = AzureADB2CDefaults.JwtBearerAuthenticationScheme;
                    option.DefaultChallengeScheme = AzureADB2CDefaults.JwtBearerAuthenticationScheme;
                    option.DefaultSignInScheme = AzureADB2CDefaults.JwtBearerAuthenticationScheme;
                })

            .AddAzureADB2CBearer(options => Configuration.Bind("AzureAdB2C", options));
        services.AddDbContext<IDataContext, DataContext>(options =>
        {
            options.UseLoggerFactory(MyLoggerFactory);
            options.EnableSensitiveDataLogging();
            options.UseSqlServer(Configuration.GetConnectionString("Dev"));
        });
        var builder = new DbContextOptionsBuilder();
        builder.UseSqlServer("fake");
        using (var context = new DataContext(builder.Options))
        {
            EfGraphQLConventions.RegisterInContainer(
                services,
                context,
                userContext => (DataContext)userContext);
        }
        foreach (var type in GetGraphQlTypes())
            services.AddTransient(type);
        services.AddCustomGraphQL(_hostingEnvironment); //left out some irrelevant code

AddCustomGraphQL:

public static IServiceCollection AddCustomGraphQL(this IServiceCollection services, IHostingEnvironment hostingEnvironment) =>
        services
            // Add a way for GraphQL.NET to resolve types.
            .AddSingleton<IDependencyResolver, GraphQLDependencyResolver>()
            .AddGraphQL(
                options =>
                {
                    var configuration = services
                        .BuildServiceProvider()
                        .GetRequiredService<IOptions<GraphQLOptions>>()
                        .Value;
                    // Set some limits for security, read from configuration.
                    options.ComplexityConfiguration = configuration.ComplexityConfiguration;
                    // Enable GraphQL metrics to be output in the response, read from configuration.
                    options.EnableMetrics = configuration.EnableMetrics;
                    // Show stack traces in exceptions. Don't turn this on in production.
                    options.ExposeExceptions = hostingEnvironment.IsDevelopment();
                }).AddGraphQLAuthorization(
                options =>
                {
                    options.AddPolicy("Authorized", p => p.RequireAuthenticatedUser());

                }
                )
            // Adds all graph types in the current assembly with a singleton lifetime.
            .AddGraphTypes()
            // Adds ConnectionType<T>, EdgeType<T> and PageInfoType.
            .AddRelayGraphTypes()
            // Add GraphQL data loader to reduce the number of calls to our repository.
            .AddDataLoader()

            // Add WebSockets support for subscriptions.
            .AddWebSockets()

            .Services;

0 个答案:

没有答案