entity framework - Dynamic database connection using Asp.net MVC and Identity2 -


i'm developing web application in asp.net mvc. application uses multiple databases. database on working on depends logged user. manage login on 2 levels:

  • level1 on "master" database have info login username/email , "specific" database use.
  • level2 on "specific" database manage users , roles identity2.

example: in "master" database have record in user table with: - username = user1 - databasetouse = "specificdb1"

in "specific" database called specificdb1, same user, have record in user table need manage user authentication , more.

what want achieve is:

  1. start website, click on login, insert username , password, click on login.
  2. search username in master database, if exist specific database name associated user.
  3. set here, dynamically, connection string "specific" database , perform identity 2 login operations.

no problems points 1 , 2. problem in point 3. use entityframework 6 code first both (master , specific) databases.

regarding configuration part of identity see in startup.auth.cs:

        app.createperowincontext(applicationdbcontext.create);         app.createperowincontext<applicationusermanager>(applicationusermanager.create);         app.createperowincontext<applicationsigninmanager>(applicationsigninmanager.create); 

should change in identity configuration?

thanks in advance.

after hours spent in searching here personal (maybe not best) working solution.

in login action of accountcontroller, after first check in "master" database, set "specific" database informations in session scope:

//save database infos in session. session.add("sqlserverinstance", masteruser.company.databaseserver); session.add("dbname", masteruser.company.databasename); 

always in accountcontroller update signinmanager , usermanager properties fixing connection string identity context:

public applicationsigninmanager signinmanager     {                 {             //set manually right connection string used identity database context.             httpcontext.getowincontext().get<applicationdbcontext>().database.connection.connectionstring = applicationdbcontext.getconnectionstring();              return _signinmanager ?? httpcontext.getowincontext().get<applicationsigninmanager>();         }         private set         {             _signinmanager = value;         }     }      public applicationusermanager usermanager     {                 {             //set manually right connection string used identity database context.             httpcontext.getowincontext().get<applicationdbcontext>().database.connection.connectionstring = applicationdbcontext.getconnectionstring();              return _usermanager ?? httpcontext.getowincontext().getusermanager<applicationusermanager>();         }         private set         {             _usermanager = value;         }     } 

and method give connection string:

/// <summary>     /// connection string getting sqlserverinstance , dbname session.     /// </summary>     public static string getconnectionstring()     {          string sqlserverinstance = default_sqlserverinstance;         if (httpcontext.current.session != null && httpcontext.current.session["sqlserverinstance"] != null)             sqlserverinstance = convert.tostring(httpcontext.current.session["sqlserverinstance"]);          string dbname = default_dbname;         if (httpcontext.current.session != null && httpcontext.current.session["dbname"] != null)             dbname = convert.tostring(httpcontext.current.session["dbname"]);          return "data source=" + sqlserverinstance + ";initial catalog=" + dbname + ";integrated security=true";      } 

hope can help.


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -