Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.MissingMethodException: Method not found: 'Void Microsoft.AspNetCore.Identity.DataProtectorTokenProvider

Stil on same project where I had issue DI Registration service type .net core 3.0. Now when that is fixed I'm getting new error. Now my code looks:

    services.AddDbContext<ApplicationIdentityDbContext>(options =>
        options.UseSqlServer(configuration.GetConnectionString("Default")));

    services.AddIdentityCore<ApplicationUser>(options =>
        {
            options.Password.RequireDigit = false;
            options.Password.RequireLowercase = false;
            options.Password.RequireNonAlphanumeric = false;
            options.Password.RequireUppercase = false;
            options.Password.RequiredLength = 4;

            options.SignIn.RequireConfirmedEmail = true;
            options.Tokens.ProviderMap.Add("CustomEmailConfirmation",
                new TokenProviderDescriptor(
                    typeof(CustomEmailConfirmationTokenProvider<IdentityUser>)));

            options.Tokens.EmailConfirmationTokenProvider = "CustomEmailConfirmation";

        })
        .AddEntityFrameworkStores<ApplicationIdentityDbContext>();

    services.AddTransient(o =>
    {
        var service = new CustomEmailConfirmationTokenProvider<IdentityUser>(o.GetService<IDataProtectionProvider>(), o.GetService<IOptions<DataProtectionTokenProviderOptions>>(), o.GetService<ILogger<DataProtectorTokenProvider<IdentityUser>>>());

        return service;
    });

And error is:

System.MissingMethodException: Method not found: 'Void Microsoft.AspNetCore.Identity.DataProtectorTokenProvider1..ctor(Microsoft.AspNetCore.DataProtection.IDataProtectionProvider, Microsoft.Extensions.Options.IOptions1)'.

like image 337
Danijel Boksan Avatar asked Nov 21 '19 11:11

Danijel Boksan


2 Answers

I've had the same issue and the problem is related to the packages itself.

Basically the problem was that many of those Microsoft.AspNetCore.* packages are now moved to Microsoft.AspNetCore.App framework, so you remove your Microsoft.AspNetCore.Identity reference and add this to your project:

<ItemGroup>
    <FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

I've noticed that my constructor (same as yours) is missing additional parameter ILogger<DataProtectorTokenProvider<TUser>>, which you can see in .NET Core 3.* versions on this link.

like image 196
zhuber Avatar answered Sep 23 '22 10:09

zhuber


I ran into the same issue while creating a custom JWT provider for password reset emails. Instead of inheriting from DataProtectorTokenProvider<TUser>, I just implemented the IUserTwoFactorTokenProvider<TUser> interface directly. You will need to implement the CanGenerateTwoFactorTokenAsync method yourself but you can avoid the hacky framework reference.

From:

public class MyTokenProvider<TUser> 
    : DataProtectorTokenProvider<TUser> where TUser : IdentityUser

To:

public class MyTokenProvider<TUser> 
    : IUserTwoFactorTokenProvider<TUser> where TUser : IdentityUser
like image 22
kwinsor5 Avatar answered Sep 24 '22 10:09

kwinsor5