How to add ASP.NET MVC5 Identity Authentication to existing database
Asked Answered
P

1

36

I am learning MVC5 identity authentication and was reading materials on www.asp.net. I have a few questions here.

  1. If I want to use identity authentication, is there a reason not to use MVC template? Or is there a reason to use empty template? MVC template also provides bootstrap.

  2. I have a database created, I want to have a DB first development. If I use the MVC template, the database for credentials will be created under the project folder. How can I merge the two database or I should just use two databases?

If my question is silly, just ignore me or give tell me what to read first. I know people in the community are good but critical. Thank you

Plusch answered 3/9, 2014 at 18:35 Comment(1)
The template sets up the pages for logging in and registering an account and some other user management stuff. If you use an empty template, you'll need to manually do that. But if you plan to use Bootstrap and don't mind the other stuff in the non-empty template, then you should feel free to use it.Duplicature
M
44

1) If I want to use identity authentication, is there a reason not to use MVC template? Or is there a reason to use empty template? MVC template also provides bootstrap.

Identity authentication, MVC template and bootstrap are not really related.

If you create new MVC 5 application, AccountController is created for you. You can use that AccountController as starting point.

If you want, you can delete the AccountController and create one yourself, but it is a lot of work.

2) I have a database created, I want to have a DB first development. If I use the MVC template, the database for credentials will be created under the project folder. How can I merge the two database or I should just use two databases?

You do not need two databases - you can place Identity tables and your custom tables in same database.

Start with Identity - let it creates database and its required tables. Then you can add custom tables to that Identity database.

Or

If you already have database with connection string, you can change the Identity connection name at the following place. Identity will create its tables inside that existing database.

enter image description here

Misshape answered 3/9, 2014 at 19:7 Comment(8)
Win, thank you very much. What if I want to use a database that have data already, and want the template to create the credential tables into that database?Plusch
Hi @Win, thank you for a great answer. But what if I've already have a database with table Users, inside that table, username and password hash is already stored, how can I tell MVC5 something like "Hey, use data in this table Users to log in for me....". How can I do that with default authentication of ASP.NET MVC5? Or I have to build everything from scratch on my own? Thank youLori
@piavgh Easiest way will be to migrate it to ASP.Net Identity. Look at Migrating an Existing Website from SQL Membership to ASP.NET IdentityMisshape
just adding this comment in case someone using a connection string generated by EF. just add a normal connection string with your db name and it will create tables into that. thanks and cheers!Tesler
any idea how we can change the schema name?Aleida
what if I want to use the identity service but I have my own user and roles table. How should I configure Identity to use the existing tables rather than creating its own ?Significant
@Avi-B Have a look at this blogpost: jamessturtevant.com/posts/ASPNET-Identity2.0-Custom-Database which details how to implement your own IUserStore and thus use your own tables. I am sure there is more to it, but I think the approach looks promising.Hairbreadth
@Misshape When I do this, I got the following error: The entity type ApplicationUser is not part of the model for the current context.Deification

© 2022 - 2024 — McMap. All rights reserved.