title: .NET Core Custom Authorize Filter
body: <p>I'm developing a new application in MVC .NET Core 2 but I have to use the existing Authentication and Authorization that's used in another application developed in MVC5. It calls a webservice with the current users details to get a list of the groups and roles the user is in, this is done in the GetUserRoles method. From googling it looks like this is done differently now compared to MVC 5. Could anyone help me implement this so I can use it like so:</p>
  
  <pre><code> [BasicAuthorisation(Roles = "SpecificUser")]
  </code></pre>
  
  <p>Here is a snippet of the filter that I'm trying to implement in .NET Core:</p>
  
  <pre><code>[AttributeUsage(AttributeTargets.All, AllowMultiple = false, Inherited = true)]
  public class BasicAuthorisation : AuthorizeAttribute, IAuthorizationFilter
  {
      MemoryCache mc = MemoryCache.Default;
      private ClaimsPrincipal _principal;
      private string _applicationName = "";
  
      public override void OnAuthorization(AuthorizationContext filterContext)
      {
          bool skipAuthorisation = filterContext.ActionDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true)
              || filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true);
  
          _principal = HttpContext.Current.User as ClaimsPrincipal;
  
          if (_principal.Identity == null)
          {
              _principal = Thread.CurrentPrincipal as ClaimsPrincipal;
          }
          _applicationName = System.Configuration.ConfigurationManager.AppSettings["ApplicationName"];
  
          if (_principal.Identity.IsAuthenticated)
          {
              if (!skipAuthorisation)
              {
                  // set userRole cache name
                  string userRoleCache = _principal.Identity.Name.Replace(DoubleSlash, string.Empty) + CacheSuffix;
  
                  List&lt;string&gt; userRoles = GetUserRoles(_principal, userRoleCache);
  
                  //get system defined Role groups
                  List&lt;string&gt; restrictedRoleGroups = Roles.Split(_doubleBar, StringSplitOptions.RemoveEmptyEntries).Select(r =&gt; r.Trim()).ToList();
  
                  // iterate groups for authorisation 
                  for (int i = 0; i &lt; restrictedRoleGroups.Count(); i++)
                  {
                      List&lt;string&gt; restrictedRoles = restrictedRoleGroups[i].Split(',').Select(r =&gt; r.Trim()).ToList();
  
                      List&lt;string&gt; matchingRoles = (from ur in userRoles
                                                    where (restrictedRoles.Any(mr =&gt; ur == mr))
                                                    select ur).ToList();
  
                      if (matchingRoles.Count().Equals(restrictedRoles.Count()))
                      {
                          _outcome = true;
                      }
  
                  }
              }
              else { _outcome = true; }
          }
  
          if (_outcome == false)
          {
              filterContext.Result = new RedirectResult("~/Message/Index");
          }
  }
  </code></pre>