8

I am trying to build new website with ASP.NET Core 1 (May 2016), and I need to implement different kind of sign-in procedures (not with SQL Server).

So I am trying implement MyOwnUserStore, where I want to override login procedures, but when I start the application the result is this exception:

InvalidOperationException: Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContext' while attempting to activate 'LoginWebApp04.Services.MyOwnUserStore'.

My code is simple, new class:

public class MyOwnUserStore : UserStore<ApplicationUser>
{
    public MyOwnUserStore(DbContext context, IdentityErrorDescriber describer = null)
        : base(context, describer)
    {
    }

    ...
}

and modification of Startup.cs:

public void ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

    var idt = services.AddIdentity<ApplicationUser, IdentityRole>();
    idt.AddUserStore<MyOwnUserStore>();  // <-------------------------- HERE
    idt.AddEntityFrameworkStores<ApplicationDbContext>();
    idt.AddDefaultTokenProviders();

    services.AddMvc();

    // Add application services.
    services.AddTransient<IEmailSender, AuthMessageSender>();
    services.AddTransient<ISmsSender, AuthMessageSender>();
}

I have changed nothing else in my new project, and the project is created as ASP.NET Core Web Application with authentication = individual user accounts.

So what I need to do more to make the first run of my app without exception?

I have tried many examples from internet but all I have found are outdated.

0

2 Answers 2

12

The DI framework doesn't know what to resolve for the user store. Change DbContext in the constructor to ApplicationDbContext

public class MyOwnUserStore : UserStore<ApplicationUser> {
    public MyOwnUserStore(ApplicationDbContext context, IdentityErrorDescriber describer = null)
        : base(context, describer) {
    }
    ...
}

Change the order of when you add the user store. You need to add entity framework stored before adding user store.

public void ConfigureServices(IServiceCollection services) {
    // Add framework services.
    services.AddDbContext<ApplicationDbContext>(options => 
        options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

    services
        .AddIdentity<ApplicationUser, IdentityRole>()
        .AddEntityFrameworkStores<ApplicationDbContext>()
        .AddUserStore<MyOwnUserStore>()  // <----MOVED AFTER ADDING EF STORES
        .AddDefaultTokenProviders();

    services.AddMvc();

    // Add application services.
    services.AddTransient<IEmailSender, AuthMessageSender>();
    services.AddTransient<ISmsSender, AuthMessageSender>();
}
Sign up to request clarification or add additional context in comments.

1 Comment

What if my UserId is int . I get error The type 'RED.Models.Security.User' cannot be used as type parameter 'TUser' in the generic type or method 'UserStore<TUser>'. There is no implicit reference conversion from 'RED.Models.Security.User' to 'Microsoft.AspNetCore.Identity.IdentityUser<string>'
0

in Core 3.1 to upper Use the code

public class MyOwnUserStore : UserStore<ApplicationUser> {
    public MyOwnUserStore(ApplicationDbContext context, IdentityErrorDescriber describer = null)
        : base(context, describer) {
    }
    ...
}


public void ConfigureServices(IServiceCollection services) {
    // Add framework services.
    services.AddDbContext<ApplicationDbContext>(options => 
        options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

    services
        .AddIdentity<ApplicationUser, IdentityRole>()
        .AddEntityFrameworkStores<ApplicationDbContext>()
        .AddUserStore<MyOwnUserStore>()  // <----MOVED AFTER ADDING EF STORES
        .AddDefaultTokenProviders();

 services.AddSingleton<MyOwnUserStore>();   <----- ADDING THIS DEP


}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.