Aspnet Core Identity Setup Without Entityframework

asp.net core identity without entity framework

Posted by Taher Chhabra on Thursday, September 22, 2016 Tags: aspnetcore   1 minute read

I was trying to setup the new Asp.net Core Identity without Entity framework. Googled a bit and found these stackoverflow answers

http://stackoverflow.com/questions/36095076/custom-authentication-in-asp-net-core http://stackoverflow.com/questions/31271600/asp-net-5-identity-custom-signinmanager http://stackoverflow.com/questions/36184856/authentication-and-authorization-without-entity-framework-in-asp-net-5-mvc-6

All the answers hints at implementing Asp.net Identity Interfaces and making changes in startup.cs. But none of these answers were working for me. Googled a bit more and found this. There is a sample project in the answer which helped a lot even though it doesn’t work with the latest asp.net version. Also after implementing the interfaces using the sample project SignInManager.IsSignedIn(User) method was always returning false. Found the fix here

In all you just need to implement IRoleStore, IUserStore and create CustomUser and CustomRole class and add these lines in Startup.cs

services.AddSingleton<IUserStore<ApplicationUser>, ExampleUserStore>();
services.AddSingleton<IRoleStore<ApplicationRole>, ExampleRoleStore>();
services.AddIdentity<ApplicationUser, ApplicationRole>().AddDefaultTokenProviders();

Here is the working sample of Asp.Net core without EF