Code Monkey home page Code Monkey logo

Comments (15)

sungam3r avatar sungam3r commented on July 18, 2024

Unfortunately there is no such possibility.

from authorization.

Ekkeir avatar Ekkeir commented on July 18, 2024

Instead of making IAuthorizationRequirement DI-dependent, use in as simple class to hold requirement-related information.
And then create a DI-dependent handler (HomeAccessHandler : AuthorizationHandler<HomeAccessRequirement>) to process the requirement.
This should help you.
P.S. Note the "Handler registration" section.

from authorization.

sungam3r avatar sungam3r commented on July 18, 2024

This is core auth package, for ASP.NET Core other one exists.

from authorization.

Ekkeir avatar Ekkeir commented on July 18, 2024

@sungam3r my bad, missed repo name.

from authorization.

sungam3r avatar sungam3r commented on July 18, 2024

@Brandon2255p how about that?

  services.AddGraphQLAuth((_, s) =>
  {
                _.AddPolicy(GraphQLPolicyNames.HomeAccess, policy =>
                {
                    policy.AddRequirement(s.GetRequiredService<BasicAccessRequirement>());
                    policy.AddRequirement(s.GetRequiredService<HomeAccessRequirement>());
                });
  };

You should of cource configure your requirements in DI-container.

from authorization.

sungam3r avatar sungam3r commented on July 18, 2024

Closed due to lack of feedback.

from authorization.

mmmikeal avatar mmmikeal commented on July 18, 2024

i tried your solution @sungam3r for a similar issue i'm having but i get System.InvalidOperationException: 'No service for type 'CoreAPI.HomeAccessRequirement' has been registered.'

from authorization.

mmmikeal avatar mmmikeal commented on July 18, 2024

services.AddSingleton<IAuthorizationRequirement>(x => new HomeAccessRequirement(x.GetRequiredService<IClient>()) );

        `services.AddGraphQLAuth((_, s) =>
        {
            _.AddPolicy("UserRolePolicy", p => p.AddRequirement(s.GetRequiredService<HomeAccessRequirement>()));
        });`

the above did not work, I am relatively new to C# is my error glaringly obvious?

from authorization.

mmmikeal avatar mmmikeal commented on July 18, 2024

I see how to do it via what @Ekkeir is saying using handlers in Asp.net core but i cannot find an example or how to create a DI handler class for core auth @sungam3r

from authorization.

joemcbride avatar joemcbride commented on July 18, 2024

AddGraphQLAuth is not a part of this project (maybe it is in the server project?), but for using DI it should look closer to this:

services.AddSingleton<HomeAccessRequirement>();


services.AddGraphQLAuth((_, s) =>
{
    _.AddPolicy("UserRolePolicy", p => p.AddRequirement(s.GetRequiredService<HomeAccessRequirement>()));
});

If you register HomeAccessRequirement as IAuthorizationRequirement, then DI does not have a registration of HomeAccessRequirement, only IAuthorizationRequirement.

Also just noticed that you may be referencing #54 (comment) for that extension.

from authorization.

mmmikeal avatar mmmikeal commented on July 18, 2024

thanks joe, not exactly what i was looking for as i have another service that needs to be injected into HomeAccessRequirement and the above did not work. I am attempting the other route which is to make my injected service available to httpcontextaccessor

from authorization.

joemcbride avatar joemcbride commented on July 18, 2024

If you have another service that needs to be injected, that other service just needs to be also registered with DI. If it doesn't work this way, then it won't work the other way either.

from authorization.

joemcbride avatar joemcbride commented on July 18, 2024
services.AddTransient<HomeAccessRequirement>();
services.AddTransient<IClient, MyClient>();


public class HomeAccessRequirement : IAuthorizationRequirement
{
    private readonly IClient client;
    public HomeAccessRequirement(IClient client)
    {
        this.client = client;
    }
    public Task Authorize(AuthorizationContext context)
    {
        ...
    }
}

services.AddGraphQLAuth((_, s) =>
{
    _.AddPolicy("UserRolePolicy", p => p.AddRequirement(s.GetRequiredService<HomeAccessRequirement>()));
});

from authorization.

mmmikeal avatar mmmikeal commented on July 18, 2024

thanks! @joemcbride i have been going in circles over and over with this. Seems like I need to look into why I can't dependency inject this...

from authorization.

joemcbride avatar joemcbride commented on July 18, 2024

@mmmikeal Here's a full working example that uses injection in a IAuthorizationRequirement.

graphql-dotnet/examples#66

from authorization.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.