Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing asp.net Identity into already existing database with entity framework

I am trying to update an older website. It already has an existing database with a user and a role table, with existing data in it.

I need to create a new web-api and a new web project and integrate ASP.NET Identity into the already existing database.

I have currently setup my solution to breakup the projects as follows:

  • Domain
  • Services
  • WEB_API
  • WEB_UI

My aim is to implement the Identity in the services layer so that both my WebAPI and MVC site can utilize the same identity mechanism.

I am fairly overwhelmed at the moment, I have read many tutorials and articles over the last day or two and have ended up with the options of either re-implementing the IUserStore and IRoleStore. And also with mapping the different entities in the OnModelCreatingMethod.

I cant seem to find a tutorial which is aimed at what I want to do. My database is to different to simply remap the names of columns, And I dont want to re-implement the entire Identity Stores as there are only a few conflicting fields. Most tuts I have found are related to using mysql instead of EF. I still want to make use Entity Framework.

Issues I have:

  • The current Users Table in the database used Int pk , not GUID
  • The Password Field uses a different Hashing algorithm. So i would need to override how Identity checks and store the password.
  • I do not have all the required Identity User fields in my database, however I am able to add new fields, I just cant change already existing fields.
  • I am making use of Database First as the DB already exists.

So basically my question is, In what direction do I need to go in order to overcome the above mentioned issues. Can I get away with changing the Database mapping? Or do I need to go as far as re implementing the User and Role Stores?

What I had initially planned was to re-implement the User and Role Stores using entity framework, And i could then make use if the DB first model classes and map the actual DB structure and fields to my ApplicationUser Fields. But this is where I thought I might be diving into cold waters, and i'm generally not a fan of reinventing the wheel if not necessary.

like image 751
Zapnologica Avatar asked Sep 29 '22 09:09

Zapnologica


1 Answers

-The first thing you should do if you haven't already is to BACK-UP your current database!

You could use code first to update an existing database.

I found this walkthrough and it seemed close to the route you're on, and I'm hoping it will help solve your problem, begin at "Migrating to Visual Studio 2013". http://www.asp.net/identity/overview/migrations/migrating-an-existing-website-from-sql-membership-to-aspnet-identity

This would create a few new tables, but not necessarily a new database.

Download Nuget packages: Microsoft.AspNet.Identity.EntityFramework, Microsoft.AspNet.Identity.Owin, Microsoft.Owin.Host.SystemWeb

Enable-migrations and run a script such as this: https://aspnet.codeplex.com/SourceControl/latest#Samples/Identity/SQLMembership-Identity-OWIN/Migrations.sql

Or you could try using a reverse POCO generator found in visual studio extensions.

(My 1st attempt at writing an answer here. Tried to clarify.)

like image 161
mymlan78 Avatar answered Oct 12 '22 23:10

mymlan78