c# - How to connect ASP.NET website to different databases? -


i preliminary apologize possibly asking question has been answered, in case please lead me relevant post, not discover myself answer.

i have asp.net website - after publishing - should able connect different databases according user's wish. in login page there should list of databases user choose , connect 1 according individual credentials. how login page should look:

enter image description here

the code login page:

public partial class login : page     {         protected void page_init(object sender, eventargs e)         {             list<string> conns = new list<string>();             foreach (connectionstringsettings conn in system.configuration.configurationmanager.connectionstrings)             {                 conns.add(conn.name);             }             listbd.datasource = conns;             listbd.databind();         }           protected void page_load(object sender, eventargs e)         {             registerhyperlink.navigateurl = "register";             forgotpasswordhyperlink.navigateurl = "forgot";             openauthlogin.returnurl = request.querystring["returnurl"];             var returnurl = httputility.urlencode(request.querystring["returnurl"]);             if (!string.isnullorempty(returnurl))             {                 registerhyperlink.navigateurl += "?returnurl=" + returnurl;             }             if (user.identity.isauthenticated && this.request.rawurl.contains("login?"))             {                 response.redirect("~/interzis.aspx");             }              else if (user.identity.isauthenticated && this.request.rawurl.contains("login"))             {                 identityhelper.redirecttoreturnurl(request.querystring["returnurl"], response);             }             applicationdbcontext db = new applicationdbcontext();         }          protected void login(object sender, eventargs e)         {             if (isvalid)             {                 var manager = context.getowincontext().getusermanager<applicationusermanager>();                 var signinmanager = context.getowincontext().getusermanager<applicationsigninmanager>();                  list<applicationuser> = manager.users.tolist();                  foreach (var user in us)                 {                     textsuccess.text += user.username + ": ";                     foreach (var role in user.roles)                     {                         textsuccess.text += role.roleid + ", ";                     }                 }                 var result = signinmanager.passwordsignin(email.text, password.text, true, shouldlockout: false);                  switch (result)                 {                     case signinstatus.success:                         panelsuccess.visible = true;                         identityhelper.redirecttoreturnurl(request.querystring["returnurl"], response);                         break;                     case signinstatus.lockedout:                         response.redirect("/account/lockout");                         break;                     case signinstatus.requiresverification:                         response.redirect(string.format("/account/twofactorauthenticationsignin?returnurl={0}&rememberme={1}",                                                          request.querystring["returnurl"],                                                         rememberme.checked),                                           true);                         break;                     case signinstatus.failure:                     default:                         failuretext.text = "Înregistrare eșuată";                         errormessage.visible = true;                         break;                 }             }         }     } 

the startup class:

public partial class startup {          public void configureauth(iappbuilder app)         {             app.createperowincontext(applicationdbcontext.create);             app.createperowincontext<applicationusermanager>(applicationusermanager.create);             app.createperowincontext<applicationrolemanager>(applicationrolemanager.create);             app.createperowincontext<applicationsigninmanager>(applicationsigninmanager.create);              app.usecookieauthentication(new cookieauthenticationoptions             {                 authenticationtype = defaultauthenticationtypes.applicationcookie,                 loginpath = new pathstring("/account/login.aspx"),                 provider = new cookieauthenticationprovider                 {                      onvalidateidentity = securitystampvalidator.onvalidateidentity<applicationusermanager, applicationuser>(                         validateinterval: timespan.fromminutes(30),                         regenerateidentity: (manager, user) => user.generateuseridentityasync(manager))                 }             });          }  } 

this current simplified version of dbcontext class:

 public class applicationdbcontext : identitydbcontext<applicationuser>     {          public applicationdbcontext()             : base("users", throwifv1schema: false)         {             configuration.proxycreationenabled = true;             configuration.lazyloadingenabled = true;         }          public static applicationdbcontext create()         {             return new applicationdbcontext();         }          // override onmodelscreating:         protected override void onmodelcreating(dbmodelbuilder modelbuilder)         {             system.data.entity.database.setinitializer<applicationdbcontext>(null);         }      } 

if add connection string parameter applicationdbcontext constructor, i'll need add in startup class here:

app.createperowincontext(() => applicationdbcontext.create(connectionstring)); 

but invoked before clicking submit button on login page, before selecting database. means there should default database app first tries connect. not want. how change situation?

secondly, piece of code should add login method in login class connecting selected database?

i add use entity framework 6 , code first migrations in order deal databases.

thank much!


Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -