Created
June 4, 2020 13:47
-
-
Save KeesCBakker/fc721222319096f43b639d42e4d0ea86 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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