I am having an issue with users and autofac. I followed a guide that said to get the current user to use in your services, you must first create a wrapper class like this:
public class PrincipalProvider : IPrincipalProvider
{
public IPrincipal User => HttpContext.Current?.User;
}
And then you need to register it as PerDependency in your module like this:
builder.RegisterType<PrincipalProvider>().As<IPrincipalProvider>().InstancePerDependency();
Then I have a simple controller:
private readonly IHealthCheckProvider _healthCheckProvider;
/// <summary>
/// Default constructor
/// </summary>
/// <param name="healthCheckProvider">The provider used to do health checks</param>
public HealthCheckController(IHealthCheckProvider healthCheckProvider)
{
_healthCheckProvider = healthCheckProvider;
}
/// <summary>
/// Lists all the current services and their status details
/// </summary>
/// <returns></returns>
[HttpGet]
[Route("")]
public async Task<IHttpActionResult> ListAsync() => Ok(await _healthCheckProvider.ListAsync());
The healthcheck provider has the principal provider injected into it and the list method tries to get the current user:
private readonly CormarConfig _config;
private readonly IHealthCheckService _healthCheckService;
private readonly IUnitOfWork _unitOfWork;
private readonly Lazy<IPrincipalProvider> _principalProvider;
public HealthCheckProvider(IUnitOfWork unitOfWork, CormarConfig config, IHealthCheckService healthCheckService, Lazy<IPrincipalProvider> principalProvider)
{
_unitOfWork = unitOfWork;
_config = config;
_healthCheckService = healthCheckService;
_principalProvider = principalProvider;
}
public async Task<IList<HealthCheck>> ListAsync()
{
var models = await _healthCheckService.List().ToListAsync();
var user = _principalProvider.Value.User;
return models;
}
The problem I have is that the user is always null. Does anyone know how I can solve this?
And just for reference sake, here is my config method in the StartupConfig.cs class:
public void Configuration(IAppBuilder app)
{
// Cors must be first, or it will not work
app.UseCors(CorsOptions.AllowAll);
CultureInfo.DefaultThreadCurrentCulture = CultureInfo.CreateSpecificCulture("en-GB");
// Get our configuration
var config = new HttpConfiguration();
var container = ConfigureInversionOfControl(app, config);
var scope = config.DependencyResolver.GetRootLifetimeScope();
var serverOptions = ConfigureOAuthTokenGeneration(app, scope);
// Configur our application
ConfigureOAuthTokenConsumption(app);
ConfigureWebApi(config, scope);
// Register the Autofac middleware FIRST. This also adds
// Autofac-injected middleware registered with the container.
app.UseAutofacMiddleware(container);
app.UseAutofacWebApi(config);
app.UseOAuthAuthorizationServer(serverOptions);
app.UseWebApi(config);
}
I don't think anything is wrong in there, but you never know.