3

What if you have your own database and a BAL (Business Access Layer) and don't want to use DefaultConnection and the template ASPNET database tables but my own user tables?

How can you use a custom database?

ConnectionString:

public class AppDbContext : IdentityDbContext<AppUser>
{
    public AppDbContext() : base("DefaultConnection")
    {
    }
}

Web.config

<add name="DefaultConnection" 
   connectionString="Data Source=(LocalDb)\v11.0;
                     AttachDbFilename=|DataDirectory|\NakedIdentity-Mvc.mdf;
                     Initial Catalog=NakedIdentity-Mvc;Integrated Security=True" 
   providerName="System.Data.SqlClient" />
radbyx
  • 9,352
  • 21
  • 84
  • 127

2 Answers2

5

You can customize your tables, your storage and your classes.
The process is not straightforward but with a little bit of work you can do that. I've answered a similar question few days ago.
You can find it here.

You can find a project on github where I've tried to customize all the tables involved in the authentication/authorization process.

This is another answer where you can read something more about using a different storage for your users and roles.

Community
  • 1
  • 1
LeftyX
  • 35,328
  • 21
  • 132
  • 193
1

You can always specify database which you want to talk - this is actually idea of connection string configuration. You need to change connectionString attribute in that way that will point to your desired DB.

Here is good source of information about connection strings

Marcin
  • 773
  • 6
  • 17
  • But my context is MVC 5 and OWIN "Individual User Account " They given template that include a Identity Database, used some specific tables. What is the requriments for my own tables? – radbyx Jun 13 '15 at 15:08
  • 1
    If you would like to use .net Identity Provider model in your custom database, you need to update your database schema according Identity Provider contract. It requires tables like `users`, `userroles`, `userlogins`, `userclimes`, `roles` (with specific colums of course), here http://www.asp.net/identity/overview/extensibility/implementing-a-custom-mysql-aspnet-identity-storage-provider you will find pretty nice example. – Marcin Jun 13 '15 at 15:18
  • Thanks it make more sense. Does that work for an existing dB too og only for newly created dB ? – radbyx Jun 13 '15 at 15:55
  • 1
    it will work for all databases until expected "contract" will be supported. This means that creation time of DB should not have matter, because for existing database you should have possibility to update/migrate its schema. Problem will appear if you can not modify your old database. – Marcin Jun 13 '15 at 16:00
  • So if we want to use our compagny existing user tables without migrating with the scheme it cannot be done? Anyways thanks alot for all the inside, it was very helpful – radbyx Jun 13 '15 at 19:40
  • 1
    without knowledge about your current data and database schema it is hard to say yes or no, but I believe that it is possible. – Marcin Jun 14 '15 at 10:04