Is it possible to move connection string for ApplicationDbContext from Web.config to some database and store it like:
- User1 | connstring1
- User2 | connstring2
and read it from database based on username entered in Login page? I tried few things but all failed. I know how to dynamically set connection string but my main problem is that ApplicationDbContext is created before Login page is shown and i cant get user data to search for connstring in database. Is there a way to call StartupAuth(app) after clicking Login and not in Startup.cs?
What I want to achieve with this: I want to have one IIS with database along side.That database to be used to point to specific IP adress(sql server) for logged user.
UPDATE
Here is my code for model first EF entity class:
public advokatEntities() : base(GetConnectionString())
{
}
public static string GetConnectionString()
{
if (HttpContext.Current.Session["advokatEntities"] != null)
{
return HttpContext.Current.Session["advokatEntities"].ToString();
}
else
{
using (redirectionEntities db = new redirectionEntities())
{
string companyName = "ines";
string server = db.redirections.Where(x => x.company == companyName).FirstOrDefault().adress;
string providerConnectionString = string.Format("server={0};user id=root;password=K0r1sn1k12345;persistsecurityinfo=True;database={1}", server,companyName);
var efConnection = new EntityConnectionStringBuilder();
efConnection.Provider = "MySql.Data.MySqlClient";
efConnection.ProviderConnectionString = providerConnectionString;
efConnection.Metadata = "res://*/Models.Advokat.csdl|res://*/Models.Advokat.ssdl|res://*/Models.Advokat.msl";
HttpContext.Current.Session["advokatEntities"] = efConnection.ToString();
return efConnection.ToString();
}
}
}
This works and I only need to change string companyName to something user typed at login.That's not big problem and I think it can be done...
I came to a problem when I tried to do this with Identity's ApplicationDbContext
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base(GetConnectionString())
{
}
public static string GetConnectionString()
{
return "server=192.168.1.122;userid=root;password=K0r1sn1k12345;database=ines;persistsecurityinfo=True";
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
}
I tried putting connection string in session but it returns null because there is no session when ApplicationDbContext is created. Can I somehow read connection string from redirectionEntities by passing companyName as a parameter from Login page?
The ideal scenario would be:
- Login page is shown
- User enters login data(Company,Username,Password) and click Login
- Application check redirectionEntities for Company and dynamically create connStrings for Identity and advokatEntities(those 2 are same except advokatEntities is database first and has metadata inside connection string)
- Authenticate user based on username password
- Store connectionStrings in user session to prevent reading from DB for each data query.
Can this be achieved at all? Or it would be easier to move all user data to redirectionEntities database?