26

I'm looking to implement ASP.net authentication via the SignInManager but without the EntityFramework. I have built my own database layer using SQLClient and want to just create whatever calls is needed in order to make ASP.net authentication work.

The code I have is as follows (executed from the Startup.cs):

// Add EF services to the services container.
services.AddEntityFramework()
    .AddSqlServer()
    .AddDbContext<OAuthAppDbContext>(opt => opt.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));

// Add Identity services to the services container.
services.AddIdentity<ApplicationUser, ApplicationRole>(options =>
{
    options.Cookies.ApplicationCookieAuthenticationScheme = "ApplicationCookie";
    options.Cookies.ApplicationCookie.AuthenticationScheme = "ApplicationCookie";
    options.Cookies.ApplicationCookie.CookieName = "oAuthInterop";
    options.Cookies.ApplicationCookie.AutomaticChallenge = true;
    options.Cookies.ApplicationCookie.AutomaticAuthenticate = true;
    options.Cookies.ApplicationCookie.DataProtectionProvider = new DataProtectionProvider(new DirectoryInfo("d:\\development\\artefacts"),
        configure =>
        {
            configure.SetApplicationName("TestAuthApp");
            //configure.ProtectKeysWithCertificate("thumbprint");
        });
})
    .AddEntityFrameworkStores<OAuthAppDbContext, int>()
    .AddDefaultTokenProviders();

and I need to remove the Entity Framework reliance (and call my own db methods for gathering user details). Has anyone else done something similar in ASP.net core?

Thanks in advance for any pointers! :-)

1
  • Just curious, have you implemented without ef? if you have any sample can you please post it. Thanks
    – Swamy
    Mar 18, 2017 at 23:52

1 Answer 1

46
+50

At the very least, you'll want to implement IUserStore<ApplicationUser>, IUserPasswordStore<ApplicationUser>, and IRoleStore<ApplicationUser> in any way you see fit, and register them with your IServiceCollection. There are a few other interfaces you might want to implement to get the full identity functionality (IUserClaimsStore, IUserPhoneNumberStore, IUserLockoutStore, etc. - you can find the whole list on GitHub).

Finally, don't forget to remove your EF service registrations!

I've put together a really basic in-memory example here. It's really quick and dirty, so I wouldn't recommend trying to take too much inspiration from it. If you really want to see a proper implementation, here is how the actual EF version is implemented!

5
  • Thanks for getting back @MatthewKing. I'm struggling to implement without an example. Do you know of any examples online using Core 1 without EF? Thanks
    – Rob
    Apr 27, 2016 at 10:03
  • 1
    I don't know any existing examples @RobMcCabe, but I'll try and put a quick example together in the next day or so if I can get some time. Apr 28, 2016 at 1:57
  • 3
    @RobMcCabe I've edited my answer to include a REALLY basic example that should get you started. It's just demonstrating the general idea using an in-memory list as the data store, but you should be able to see how you'd plug in your own database from there! I hope this helps. Apr 28, 2016 at 5:56
  • 2
    @RobMcCabe I've also added a link to the official EF implementation. It's probably a better example of how to do this. Apr 28, 2016 at 11:34
  • 1
    Excellent @MatthewKing! Thanks so much for the working example (I know this needs to be extended and implemented as I need but this is a huge help!). Thanks!
    – Rob
    Apr 28, 2016 at 23:16

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.