Code Monkey home page Code Monkey logo

Comments (9)

sungam3r avatar sungam3r commented on July 18, 2024 2

Instead of

   services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();

you can use special method

  services.AddHttpContextAccessor();

from Microsoft.AspNetCore.Http what does exacly the same thing.

from authorization.

joemcbride avatar joemcbride commented on July 18, 2024 1

Here's an alternative way to do it:

public class MyReq : IAuthorizationRequirement
{
    private readonly IHttpContextAccessor accessor;

    public MyReq(IHttpContextAccessor accessor)
    {
        this.accessor = accessor;
    }

    public Task Authorize(AuthorizationContext context)
    {
        return Task.CompletedTask;
    }
}

public static class GraphQLAuthExtensions
{
    public static void AddGraphQLAuth(this IServiceCollection services, Action<AuthorizationSettings, IServiceProvider> configure)
    {
        services.AddHttpContextAccessor();
        services.AddTransient<IAuthorizationEvaluator, AuthorizationEvaluator>();
        services.AddTransient<IValidationRule, AuthorizationValidationRule>();
        services.AddTransient<MyReq>();

        services.TryAddTransient(s =>
        {
            var authSettings = new AuthorizationSettings();
            configure(authSettings, s);
            return authSettings;
        });
    }
}

public void ConfigureServices(IServiceCollection services)
{
    services.AddGraphQLAuth((_, s) =>
    {
        _.AddPolicy("SomePolicy", p => p.AddRequirement(s.GetService<MyReq>()));
    });
}

from authorization.

joacoleza avatar joacoleza commented on July 18, 2024

I ended adding to my GraphQLUserContext (which implements IProvideClaimsPrincipal) a HttpContext property. This property is being set when constructing it on the GraphQLUserContextBuilder (as HttpContext is a parameter of the method BuildUserContext).

Then, when implementing my IAuthorizationRequirement on the Authorize method I can access the registered service from the HttpContext doing:

HttpContext httpContext = ((GraphQLUserContext)context.UserContext).HttpContext;
var myService = (IMyService)httpContext.RequestServices.GetService(typeof(IMyService));

Is there a better way of doing so?

from authorization.

joemcbride avatar joemcbride commented on July 18, 2024

I updated IAuthorizationEvaluator in the example to be registered as Transient. Otherwise AuthorizationSettings would become a singleton indirectly.

from authorization.

sungam3r avatar sungam3r commented on July 18, 2024

Enabling scope validation on DI-container may help to find such problems.

from authorization.

joacoleza avatar joacoleza commented on July 18, 2024

Here's an alternative way to do it:

public class MyReq : IAuthorizationRequirement
{
    private readonly IHttpContextAccessor accessor;

    public MyReq(IHttpContextAccessor accessor)
    {
        this.accessor = accessor;
    }

    public Task Authorize(AuthorizationContext context)
    {
        return Task.CompletedTask;
    }
}

public static class GraphQLAuthExtensions
{
    public static void AddGraphQLAuth(this IServiceCollection services, Action<AuthorizationSettings, IServiceProvider> configure)
    {
        services.AddHttpContextAccessor();
        services.AddTransient<IAuthorizationEvaluator, AuthorizationEvaluator>();
        services.AddTransient<IValidationRule, AuthorizationValidationRule>();
        services.AddTransient<MyReq>();

        services.TryAddTransient(s =>
        {
            var authSettings = new AuthorizationSettings();
            configure(authSettings, s);
            return authSettings;
        });
    }
}

public void ConfigureServices(IServiceCollection services)
{
    services.AddGraphQLAuth((_, s) =>
    {
        _.AddPolicy("SomePolicy", p => p.AddRequirement(s.GetService<MyReq>()));
    });
}

Thanks! I'll do it this way

from authorization.

mmmikeal avatar mmmikeal commented on July 18, 2024

this is incredibly helpful @joacoleza , do you have an example of how you added the service you want injected to the httpcontext and how you retrieve it out of the httpcontext? i am getting some type errors

from authorization.

mmmikeal avatar mmmikeal commented on July 18, 2024

for example in my AddGraphQLAuth i have

services.AddHttpContextAccessor(); services.AddTransient<MyServiceToBeInjected>();

and in my requirement i attempt to access it by
var service = accessor.HttpContext.RequestServices.GetService<MyServiceToBeInjected>();

from authorization.

joacoleza avatar joacoleza commented on July 18, 2024

this is incredibly helpful @joacoleza , do you have an example of how you added the service you want injected to the httpcontext and how you retrieve it out of the httpcontext? i am getting some type errors

I no longer have access to the code where this was implemented. Sorry I couldn’t be more help.

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.