Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KeesCBakker/fc721222319096f43b639d42e4d0ea86 to your computer and use it in GitHub Desktop.
Save KeesCBakker/fc721222319096f43b639d42e4d0ea86 to your computer and use it in GitHub Desktop.
public static class HiddenCookieExtensions
{
public static IServiceCollection AddScopedByHiddenCookie<TService, THiddenImplementation, TDefaultImplementation>(this IServiceCollection services)
where TService: class
where THiddenImplementation: TService
where TDefaultImplementation: TService
{
services.AddScoped<TService>(provider =>
{
var context = provider.GetRequiredService<IHttpContextAccessor>();
var isHidden = context?.HttpContext?.Request.Cookies.ContainsKey("hidden") == true;
if (isHidden)
{
return provider.GetRequiredService<THiddenImplementation>();
}
return provider.GetRequiredService<TDefaultImplementation>();
});
return services;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment