找不到 DataProtectorTokenProvider 构造函数方法
DataProtectorTokenProvider constructor method not found
我一直在 .NET Core 2.2 中使用两个具有标识的自定义令牌提供程序。迁移到 3.1 后我得到这个异常
System.Exception: Could not resolve a service of type 'Microsoft.AspNetCore.Identity.UserManager`1[[App.Domain.Entities.AppUser, App.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' for the parameter 'userManager' of method 'Configure' on type 'App.API.Startup'.
---> System.MissingMethodException: Method not found: 'Void Microsoft.AspNetCore.Identity.DataProtectorTokenProvider`1..ctor(Microsoft.AspNetCore.DataProtection.IDataProtectionProvider, Microsoft.Extensions.Options.IOptions`1<Microsoft.AspNetCore.Identity.DataProtectionTokenProviderOptions>)'.
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
我认为这是 UserManager
构造函数中他们试图解析所有令牌提供者的部分。
这里是身份配置
namespace App.Identity
{
public static class IdentityExtensions
{
public static IServiceCollection AddAppIdentity(this IServiceCollection services, IConfiguration configuration)
{
services.AddIdentity<AppUser, AppRole>(options =>
{
options.Password.RequireDigit = true;
options.Password.RequireLowercase = true;
options.Password.RequireNonAlphanumeric = true;
options.Password.RequireUppercase = true;
options.Tokens.EmailConfirmationTokenProvider = EmailConfirmationTokenProviderOptions.ProviderName;
})
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders()
.AddEmailConfirmationTokenProvider()
.AddMagicLinkLoginTokenProvider();
return services;
}
private static IdentityBuilder AddMagicLinkLoginTokenProvider(this IdentityBuilder builder)
{
return builder.AddTokenProvider(
MagicLinkLoginTokenProviderOptions.ProviderName,
typeof(MagicLinkLoginTokenProvider<>).MakeGenericType(builder.UserType)
);
}
private static IdentityBuilder AddEmailConfirmationTokenProvider(this IdentityBuilder builder)
{
return builder.AddTokenProvider(
EmailConfirmationTokenProviderOptions.ProviderName,
typeof(EmailConfirmationTokenProvider<>).MakeGenericType(builder.UserType)
);
}
}
}
更新:
经过一些调查,我注意到最新的 DataProtectorTokenProvider
具有构造函数签名,其中包括 ILogger
,而我的 (Microsoft.AspNetCore.Identity v2.2) 只有 dataProtectionProvider
和options
。我引用了错误的包还是?
基本上,问题在于许多 Microsoft.AspNetCore.*
包现在已移至 Microsoft.AspNetCore.App
框架,因此您删除 Microsoft.AspNetCore.Identity
引用并将其添加到您的项目
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
之后它应该从所有框架 dll 所在的共享文件夹中解析它。
我一直在 .NET Core 2.2 中使用两个具有标识的自定义令牌提供程序。迁移到 3.1 后我得到这个异常
System.Exception: Could not resolve a service of type 'Microsoft.AspNetCore.Identity.UserManager`1[[App.Domain.Entities.AppUser, App.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' for the parameter 'userManager' of method 'Configure' on type 'App.API.Startup'.
---> System.MissingMethodException: Method not found: 'Void Microsoft.AspNetCore.Identity.DataProtectorTokenProvider`1..ctor(Microsoft.AspNetCore.DataProtection.IDataProtectionProvider, Microsoft.Extensions.Options.IOptions`1<Microsoft.AspNetCore.Identity.DataProtectionTokenProviderOptions>)'.
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
我认为这是 UserManager
构造函数中他们试图解析所有令牌提供者的部分。
这里是身份配置
namespace App.Identity
{
public static class IdentityExtensions
{
public static IServiceCollection AddAppIdentity(this IServiceCollection services, IConfiguration configuration)
{
services.AddIdentity<AppUser, AppRole>(options =>
{
options.Password.RequireDigit = true;
options.Password.RequireLowercase = true;
options.Password.RequireNonAlphanumeric = true;
options.Password.RequireUppercase = true;
options.Tokens.EmailConfirmationTokenProvider = EmailConfirmationTokenProviderOptions.ProviderName;
})
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders()
.AddEmailConfirmationTokenProvider()
.AddMagicLinkLoginTokenProvider();
return services;
}
private static IdentityBuilder AddMagicLinkLoginTokenProvider(this IdentityBuilder builder)
{
return builder.AddTokenProvider(
MagicLinkLoginTokenProviderOptions.ProviderName,
typeof(MagicLinkLoginTokenProvider<>).MakeGenericType(builder.UserType)
);
}
private static IdentityBuilder AddEmailConfirmationTokenProvider(this IdentityBuilder builder)
{
return builder.AddTokenProvider(
EmailConfirmationTokenProviderOptions.ProviderName,
typeof(EmailConfirmationTokenProvider<>).MakeGenericType(builder.UserType)
);
}
}
}
更新:
经过一些调查,我注意到最新的 DataProtectorTokenProvider
具有构造函数签名,其中包括 ILogger
,而我的 (Microsoft.AspNetCore.Identity v2.2) 只有 dataProtectionProvider
和options
。我引用了错误的包还是?
基本上,问题在于许多 Microsoft.AspNetCore.*
包现在已移至 Microsoft.AspNetCore.App
框架,因此您删除 Microsoft.AspNetCore.Identity
引用并将其添加到您的项目
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
之后它应该从所有框架 dll 所在的共享文件夹中解析它。