哪个包包含 IUserEmailStore 的实现

问题描述 投票:0回答:1

我正在开发一个在c#中使用短信实现两因素身份验证的项目。我正在尝试在我的控制器中使用名为

IUserEmailStore
的接口 - 所以我的控制器的构造函数如下所示:

public AccountsController(
            SignInManager<User> signInManager,
            UserManager<User> userManager,
            IUserStore<User> userStore,
            IUserEmailStore<User> emailStore,
            RepositoryContext context,
            IMapper mapper, 
            UrlEncoder urlEncoder,
            JwtHandler jwtHandler)
        {
            _signInManager = signInManager;
            _userStore = userStore;
            _userManager = userManager;
            _emailStore = emailStore;
            _mapper = mapper;
            _urlEncoder = urlEncoder;
            _jwtHandler = jwtHandler;
            _context = context;
        }

I am getting exception :
System.InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Identity.IUserEmailStore`1[TwoFactorAuthenticationGoogleAuthenticatorAngular.Models.User]' while attempting to activate 'TwoFactorAuthenticationGoogleAuthenticatorAngular.Controllers.AccountsController'.
         at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, Boolean isDefaultParameterRequired)
         at lambda_method10(Closure, IServiceProvider, Object[])
         at Microsoft.AspNetCore.Mvc.Controllers.ControllerFactoryProvider.<>c__DisplayClass6_0.<CreateControllerFactory>g__CreateController|0(ControllerContext controllerContext)
         at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
         at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
      --- End of stack trace from previous location ---
         

我已经包含了我认为相关的每个包 - 包括

Microsoft.Extensions.Identity.Core
接口的 Microsoft 文档中引用的
IUserEmailStore<TUser>

我包含的套餐有:

我已经没有主意了。有人可以帮忙吗?

我的 Program.cs 看起来像这样:

var builder = WebApplication.CreateBuilder(args);

var allowSpecificOrigins = "two_factor_auth_cors";

builder.Services.AddDbContext<RepositoryContext>(options =>
    options.UseSqlite(builder.Configuration.GetConnectionString("sqliteConnection") ?? throw new InvalidOperationException("Connection string 'sqliteConnection' not found.")));

builder.Services.AddDefaultIdentity<User>(options => options.SignIn.RequireConfirmedAccount = false).AddEntityFrameworkStores<RepositoryContext>();
    
var jwtSettings = builder.Configuration.GetSection("JwtSettings");
builder.Services.AddAuthentication(opt =>
{
    opt.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
    opt.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(options =>
{
    options.TokenValidationParameters = new TokenValidationParameters
    {
        ValidateIssuer = true,
        ValidateAudience = true,
        ValidateLifetime = true,
        ValidateIssuerSigningKey = true,
        ValidIssuer = jwtSettings["validIssuer"],
        ValidAudience = jwtSettings["validAudience"],
        IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8
            .GetBytes(jwtSettings.GetSection("securityKey").Value))
    };
});


builder.Services.AddScoped<JwtHandler>();

builder.Services.AddAutoMapper(typeof(MappingProfile));
builder.Services.AddControllers();
builder.Services.AddSwaggerGen();

// JD
builder.Services.AddTransient<ISmsSender, MessageSender>();
builder.Services.Configure<SMSOptions>(builder.Configuration);
//JD

var app = builder.Build();

app.UseDeveloperExceptionPage();
app.UseSwagger();

app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "WebApplication5 v1"));
// Register the Swagger generator, defining 1 or more Swagger documents  



app.UseCors(allowSpecificOrigins);
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.Run();
c# visual-studio two-factor-authentication
1个回答
0
投票

我通过寻找其他项目如何解决这个问题来解决这个问题。所以基本上我不会将

IUserEmailStore
直接输入到我的控制器中。我将 DI 改为
IUserStore
,并将其转换为
IUserEmailStore
- 因为 Microsoft 确实返回了
IUserEmailStore
IUserEmailStore
源自
IUserStore
)。就是这样。 希望它对某人有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.