Migrating solution from .NET Core 2.2 to .NET Core 7.0

While migrating the code base from .NET Core 2.2 to .NET Core 7.0 , we are stuck in 1 issue. There is a namespace which is being used Microsoft.AspNetCore.Http.Internal in AuthenticationFilterProvider.cs.

using Microsoft.AspNetCore.Mvc.Internal;

namespace ProjectName.WebAPI.LegacyAuth
{
    public class FilterProviderOption
    {
        public string RoutePrefix { get; set; }
        public AuthorizeFilter Filter { get; set; }
    }

    public class AuthenticationFilterProvider : DefaultFilterProvider
    {
        private readonly FilterProviderOption[] _options;
        public AuthenticationFilterProvider(params FilterProviderOption[] options)
        {
            _options = options ?? throw new ArgumentNullException(nameof(options));
        }
 public override void ProvideFilter(FilterProviderContext context, FilterItem filterItem)
        {
            var route = context.ActionContext.ActionDescriptor.AttributeRouteInfo.Template;

            var filter = _options.FirstOrDefault(_options => route.StartsWith(_options.RoutePrefix, StringComparison.Ordinal))?.Filter;

            if (filter != null)
            {
                if (context.Results.All(r => r.Descriptor.Filter != filter))
                {
                    context.Results.Add(new FilterItem(new FilterDescriptor(filter, (int)FilterScope.Controller)));
                }
            }

            base.ProvideFilter(context, filterItem);
        }
}
}

We are using the custom filter – AuthenticationFilterProvider which inherits from DefaultFilterProvider (out of box) . This is strictly internal and hence the inheritance fails.

Error – “DefaultFilterProvider is inaccessible due to its protection level”

We tried using this but to no avail.

  <ItemGroup>
    <PackageReference Include="CSharpFunctionalExtensions" Version="2.29.1" />
    <FrameworkReference Include="Microsoft.AspNetCore.App" />
  </ItemGroup>  

Kindly let me know if any fix is possible for this.

  • 3

    what are you doing in that filter and using internal classes? they are bound to break. perhaps there’s a better way for you to accomplish what you want

    – 

  • 3

    Yeah, you should never have used an internal namespace in the first place.

    – 

  • 5

    Also, please don’t post images of code and error messages. Instead post the actual text here.

    – 

  • FrameworkReference is useless if your project already targets Web SDK.

    – 

  • 1

    Might you please edit your question to include your code and errors as text rather than as screenshots? It’s requested here not to to use images for this purpose, see Discourage screenshots of code and/or errors and Why not upload images of code on SO when asking a question for why.

    – 

.NET Core 3.0 has the “Pubternal” APIs removed breaking change:

To better maintain the public API surface of ASP.NET Core, most of the types in *.Internal namespaces (referred to as “pubternal” APIs) have become truly internal. Members in these namespaces were never meant to be supported as public-facing APIs. The APIs could break in minor releases and often did. Code that depends on these APIs breaks when updating to ASP.NET Core 3.0.

Old behavior
The affected APIs are marked with the public access modifier and exist in *.Internal namespaces.

New behavior
The affected APIs are marked with the internal access modifier and can no longer be used by code outside that assembly.

And Microsoft.AspNetCore.Http.Internal is one of them based on the list there.

Rewrite the code. Or alternatively you can always copy (at your own risk) the code for DefaultFilterProvider from github.

Leave a Comment