-
-
Notifications
You must be signed in to change notification settings - Fork 38
DI in AuthorizationRequirement #73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Unfortunately there is no such possibility. |
Instead of making |
This is core auth package, for ASP.NET Core other one exists. |
@sungam3r my bad, missed repo name. |
@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. |
Closed due to lack of feedback. |
i tried your solution @sungam3r for a similar issue i'm having but i get |
the above did not work, I am relatively new to C# is my error glaringly obvious? |
services.AddSingleton<HomeAccessRequirement>();
services.AddGraphQLAuth((_, s) =>
{
_.AddPolicy("UserRolePolicy", p => p.AddRequirement(s.GetRequiredService<HomeAccessRequirement>()));
}); If you register Also just noticed that you may be referencing #54 (comment) for that extension. |
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 |
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. |
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>()));
}); |
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... |
@mmmikeal Here's a full working example that uses injection in a |
I am struggling to figure out how to inject into an AuthorizationRequirement
I am trying to inject IClient in the following code example.
When setting up the policy, I have to use a constructor which means I am not using DI
How can I achieve DI to inject the IClient which is needed for authorization
The text was updated successfully, but these errors were encountered: