Skip to content

HttpContext null from injected IHttpContextAccessor #1257

@GarageWeb

Description

@GarageWeb

I have an authorization handler that I am injecting a custom personService into. I have wired up both the handler and the personService through the default DI. The person service IhttpContextAccessor works fine elsewhere in my application, but when it is called from the authorization handler, httpcontext is always null.

Here is the AuthHandler:

public class NoChildrenRequirement : AuthorizationHandler<NoChildrenRequirement>, IAuthorizationRequirement
{
    private readonly IPersonService _personService;

    public NoChildrenRequirement(IPersonService personService)
    {
        _personService = personService;
    }

    protected override void Handle(AuthorizationContext context, NoChildrenRequirement requirement)
    {
        var user = context.User;
        var identity = user.Identity;
        var isAuthenticated = identity.IsAuthenticated;

        var currentPerson = _personService.CurrentPerson();

        if (isAuthenticated && currentPerson != null && !currentPerson.IsAChild)
        {
            context.Succeed(requirement);
            return;
        }
        context.Fail();
    }
}

Here is where I wire it up in startup:

     services.AddTransient<NoChildrenRequirement>();
        services.Configure<AuthorizationOptions>(options =>
        {
            options.AddPolicy("NoChildren", policy => policy.Requirements.Add(
                services.BuildServiceProvider().GetRequiredService<NoChildrenRequirement>()));
        });

Here is the start of the personservice:

     public class PersonService : IPersonService
{
    private readonly IPersonRepository _repository;
    private readonly IMemoryCache _memoryCache;
    private readonly IConfigService _configService;
    private readonly IHttpContextAccessor _context;
    private readonly IResourceService _resourceService;
    private readonly string _cacheKey = "Person_";

    public PersonService(IPersonRepository repository, IMemoryCache memoryCache, 
        IConfigService configService, IHttpContextAccessor context, 
        IResourceService resourceService)
    {
        _repository = repository;
        _memoryCache = memoryCache;
        _configService = configService;
        _context = context;
        _resourceService = resourceService;
    }

Here is where I wire up person service in startup:

    services.AddTransient<IPersonService, PersonService>();

If I access : IHttpContextAccessor _context; in person service anywhere else in the application, it works, when I call personservice from the authhandler, all of the other dependencies wire up correctly and even the concrete HttpContextAccessor value is not null, but HttpContext within HttpContextAccessor is null.

v1.0.0-rc1-final

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions