Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use ASP.net Core 1 "SignInManager" without EntityFramework

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! :-)

like image 381
Rob McCabe Avatar asked Apr 22 '16 12:04

Rob McCabe


People also ask

Can I use EF 6 in .NET core?

You can't put an EF6 context in an ASP.NET Core project because . NET Core projects don't support all of the functionality that EF6 commands such as Enable-Migrations require.

What is Entityframework in asp net?

Entity Framework (EF) is an object-relational mapper that enables . NET developers to work with relational data using domain-specific objects. It eliminates the need for most of the data-access code that developers usually need to write. Get it: Add this to your project and start taking advantage of this powerful O/RM.


1 Answers

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!

like image 114
Matthew King Avatar answered Oct 03 '22 21:10

Matthew King