,net core 3.1 AddAuthorization method not found

Viewed 3176

I'm migrating from .net core 2.2 to 3.1 but I got this error in runtime

System.MissingMethodException: 'Method not found: 'Microsoft.Extensions.DependencyInjection.IServiceCollection Microsoft.Extensions.DependencyInjection.AuthorizationServiceCollectionExtensions.AddAuthorization(Microsoft.Extensions.DependencyInjection.IServiceCollection, System.Action`1<Microsoft.AspNetCore.Authorization.AuthorizationOptions>)'.'

I made sure that I include all the needed nuget libraries

<ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Authorization" Version="3.1.12" />
    <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0-preview.2.21154.6" />
    <PackageReference Include="Signed.IdentityModel.AspNetCore.OAuth2Introspection" Version="3.5.0" /> 
    <PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
    <PackageReference Include="System.Text.Encoding.CodePages" Version="5.0.0" /> 
    <PackageReference Include="Microsoft.AspNetCore.SpaServices.Extensions" Version="2.0.0" />
</ItemGroup>

I even tried adding ( not convinced it's needed)

and in startup

 services.AddMvc(options => {
                options.AddOAuth2AuthorizationPolicy(); 
            });
services.AddAuthorization();

not sure what's missing

1 Answers

Add a reference to Microsoft.AspNetCore.Authorization.Policy or use AddAuthorizationCore instead. The change is described in ASP.NET Core breaking changes for versions 3.0 and 3.1 list, quote:

The core AddAuthorization methods that used to reside in Microsoft.AspNetCore.Authorization were renamed to AddAuthorizationCore. The old AddAuthorization methods still exist, but are in the Microsoft.AspNetCore.Authorization.Policy assembly instead.

Related