Mech0z
Mech0z

Reputation: 3647

Getting error with aspnet core 2.1 action filter due to missing suitable constructor

I have made a claims filter

public class ClaimRequirementAttribute : TypeFilterAttribute
{
    public ClaimRequirementAttribute(string claimType, ClaimRoles claimValue) : base(typeof(ClaimRequirementFilter))
    {
        Arguments = new object[] {new Claim(claimType, claimValue.ToString()) };
    }
}

public class ClaimRequirementFilter : IAuthorizationFilter
{
    public void OnAuthorization(AuthorizationFilterContext context)
    {
        var headers = context.HttpContext.Request.Headers;

        var tokenSuccess = headers.TryGetValue("Token", out var token);

        var emailSuccess = headers.TryGetValue("Email", out var email);

        var deviceNameSuccess = headers.TryGetValue("DeviceName", out var deviceName);

        if (tokenSuccess && emailSuccess && deviceNameSuccess)
        {
            var accountLogic = context.HttpContext.RequestServices.GetService<IAccountLogic>();

            var hasClaim = accountLogic.ValidateLogin(email, token, deviceName).Result.Success;

            if (!hasClaim)
            {
                context.HttpContext.ForbidAsync();
            }
        }
        else
        {
            context.HttpContext.ForbidAsync();
        }
    }

}

I have registered the filter in my startup

    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<ConnectionStringsSettings>(Configuration.GetSection("ConnectionStrings"));
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

        services.AddScoped<ClaimRequirementFilter>();

But I get this error when I navigate to an action that uses the filter

[HttpPost]
[ClaimRequirement("Permission", ClaimRoles.Admin)]
public async Task ResetLeaderboard()

InvalidOperationException: A suitable constructor for type 'Foosball.Logic.ClaimRequirementFilter' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor

github: https://github.com/Mech0z/Foosball/tree/core2.1/Foosball

Upvotes: 1

Views: 1155

Answers (1)

Set
Set

Reputation: 49789

As your code has

Arguments = new object[] {new Claim(claimType, claimValue.ToString()) };

you need to add the following constructor:

public ClaimRequirementFilter(Claim claim)
{

}

That is because the internal constructor resolving logic uses TypeFilterAttribute.Argument property to decide what constructor to use for instantiation.

Upvotes: 1

Related Questions