Created
May 20, 2020 05:17
ASP.NET Core Feature Management - MobileFilter.cs
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
namespace FeatureManagementSample | |
{ | |
using Microsoft.AspNetCore.Http; | |
using Microsoft.Extensions.Configuration; | |
using Microsoft.FeatureManagement; | |
using System; | |
using System.Threading.Tasks; | |
public class MobileFilter : IFeatureFilter | |
{ | |
private readonly IHttpContextAccessor _httpContextAccessor; | |
public MobileFilter(IHttpContextAccessor httpContextAccessor) | |
{ | |
_httpContextAccessor = httpContextAccessor; | |
} | |
public Task<bool> EvaluateAsync(FeatureFilterEvaluationContext context) | |
{ | |
var isEnabled = false; | |
var settings = context.Parameters.Get<MobileFilterSettings>(); | |
var userAgent = _httpContextAccessor.HttpContext.Request.Headers["User-Agent"].ToString(); | |
foreach (var item in settings.Allowed) | |
{ | |
if (userAgent.Contains(item, StringComparison.OrdinalIgnoreCase)) | |
{ | |
isEnabled = true; | |
break; | |
} | |
} | |
return Task.FromResult(isEnabled); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment