Using the method recommended by @Gigano in this response ( Is it possible to leave connectionString dynamically? ), I was able to get a dynamic connection, and using the idea of mapping a context with the user data as the name of the bank, host, etc ... I encountered another problem: Each user logged in, the data contained in the context is overwritten by the data of the most recent authenticated user.
What is the elegant way of creating a context for each logged-in user on the system where there is no interference from the other?
Cookies? Sessions?
Update
The change of bank is made all the time the entity is called:
public EntidadesDCSystem(string str = "Conexao") : base(str)
{
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
var user = HttpContext.Current.Session["usrProfile"] as UsuarioContexto;
this.MudarBanco(database: user.database, configConnectionStringName: "Conexao");
}
}
I did so (with session), as it was the only way I found that user data would not merge. It follows where I set this session, which is the moment of user login.
[HttpPost]
public ActionResult Login(AuthLogin form, string returnUrl)
{
...
//FormsAuthentication.SetAuthCookie(usr.nome, true);
var userData = usr.descricao + '|' + usr.usuarioid;
var ticket = new FormsAuthenticationTicket(1,
usr.nome,
DateTime.Now,
DateTime.Now.AddHours(8),
true,
userData);
var encTicket = FormsAuthentication.Encrypt(ticket);
var userProfile = new UsuarioContexto
{
nome = usr.nome,
userid = usr.usuarioid,
database = usr.conexao
};
Session["usrProfile"] = userProfile;
...
}