Created
January 19, 2020 13:09
-
-
Save kasunkv/9a634100ab849541c4bf168158afcba6 to your computer and use it in GitHub Desktop.
Custom Implementation of IFeatureFilter
This file contains hidden or 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
using Microsoft.AspNetCore.Http; | |
using Microsoft.Extensions.Configuration; | |
using Microsoft.Extensions.Logging; | |
using Microsoft.FeatureManagement; | |
using System; | |
using System.Linq; | |
using System.Threading.Tasks; | |
namespace MusicStore.Web.FeatureManagement.Filters | |
{ | |
[FilterAlias(FilterAlias)] | |
public class BrowserFilter : IFeatureFilter | |
{ | |
private const string FilterAlias = "MusicStore.Browser"; | |
private const string Chrome = "Chrome"; | |
private const string Firefox = "Firefox"; | |
private const string IE = "MSIE"; | |
private readonly ILogger _logger; | |
private readonly IHttpContextAccessor _httpContextAccessor; | |
public BrowserFilter(IHttpContextAccessor httpContextAccessor, ILoggerFactory loggerFactory) | |
{ | |
_httpContextAccessor = httpContextAccessor; | |
_logger = loggerFactory.CreateLogger<BrowserFilter>(); | |
} | |
public Task<bool> EvaluateAsync(FeatureFilterEvaluationContext context) | |
{ | |
var settings = context.Parameters.Get<BrowserFilterSettings>() ?? new BrowserFilterSettings(); | |
if (settings.AllowedBrowsers.Any(browser => browser.Contains(Chrome, StringComparison.OrdinalIgnoreCase)) && IsBrowser(Chrome)) | |
{ | |
return Task.FromResult(true); | |
} | |
if (settings.AllowedBrowsers.Any(browser => browser.Contains(Firefox, StringComparison.OrdinalIgnoreCase)) && IsBrowser(Firefox)) | |
{ | |
return Task.FromResult(true); | |
} | |
if (settings.AllowedBrowsers.Any(browser => browser.Contains(IE, StringComparison.OrdinalIgnoreCase)) && IsBrowser(IE)) | |
{ | |
return Task.FromResult(true); | |
} | |
_logger.LogWarning($"The AllowedBrowsers list is empty or the current browser is not enabled for this feature"); | |
return Task.FromResult(false); | |
} | |
private string GetUserAgent() | |
{ | |
var userAgent = _httpContextAccessor.HttpContext.Request.Headers["User-Agent"]; | |
return userAgent; | |
} | |
private bool IsBrowser(string browser) | |
{ | |
var userAgent = GetUserAgent(); | |
return userAgent != null && userAgent.Contains(browser, StringComparison.OrdinalIgnoreCase); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment