You normally can’t get the current request as a singleton and should avoid relying on doing so, it’s an ASP.NET feature that has been disabled by default in .NET Core. Instead you should look at populating anything you need in a custom IAuthSession
and retrieve it from that.
This code doesn’t look like you’re using the AuthenticateService
at all, you’re just using it as an indirect way of accessing the IRequest
. If you register IHttpContextAccessor
in your .NET Core Startup class, e.g:
public override void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
}
You can access an IRequest
instance with HostContext.GetCurrentRequest()
, e.g:
var request = HostContext.GetCurrentRequest();
var token = request.GetAuthenticationToken();
return AuthenticationToken.IsValid(token);
But I would still avoid relying on accessing the current request via a singleton.