I know this is repeated question and even here but I have tried all solutions without no sense. I have the main DB where the user check if found and return by his own or related database that should change the connection string to be current used one. My problem is that despite it's got the database correct but a dbcontext works on the main database it diverts again I don't why.
My applicationdbcontext is
public ApplicationDbContext(string connectionString)
: base(string.IsNullOrEmpty(connectionString) ? "DefaultConnection" : connectionString, throwIfV1Schema: false)
{
this.Database.CommandTimeout = 600;
}
public static ApplicationDbContext Create(string dbCatlogConn)
{
return new ApplicationDbContext(ConString.dbCatlogConn);
}
and this is my public class
public class ConString
{
public static string dbCatlogConn { get; set; }
}
This is my login in accountcontroller class
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl, string language = Config.DefaultLanguage)
{
try
{
System.Web.Helpers.AntiForgery.Validate();
}
catch
{
return RedirectToRoute("Login", new { language, returnUrl });
}
dbName = model.username;
Session["dbName"] = dbName;
var dbname = db.SchoolLists
.Where(t => (t.dbname == dbName))
.Select(t => new { ConnString = t.ConnectionString }).ToList();
// new conection
dbConnectionString = "";
Session["ConnectionString"] = dbConnectionString;
db = new ApplicationDbContext();
UserManager.PasswordHasher = new CustomPasswordHasher();
bool CheckConnResult = db.Database.Exists();
// code here
var user = db.Users.Where(e => e.UserName.ToLower() == model.UserName.ToLower()).FirstOrDefault();
var result = new SignInStatus();
if (user == null)
result = SignInStatus.Failure;
else
{
string dbPassword = dal.DecryptPassword(user.AnotherUsername, user.AnotherSalt, user.PasswordHash);
var status = UserManager.PasswordHasher.VerifyHashedPassword(dbPassword, model.Password);
if (status == PasswordVerificationResult.Success)
// error here
result = await SignInManager.PasswordSignInAsync(model.UserName, user.PasswordHash, model.RememberMe, shouldLockout: false);
// result = SignInStatus.Success;
else
result = SignInStatus.Failure;
}
switch (result)
{
case SignInStatus.Success:
if (user != null)
{
if (user.Disabled == true)
{
AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
ModelState.AddModelError("", language == "Invalid login attempt.");
// rest the connection to default
// = ConString.Mainbd;
return View(model);
//return View("Lockout");
}
else
{
}
}
return RedirectToLocal(returnUrl);
case SignInStatus.LockedOut:
return View("Lockout");
case SignInStatus.RequiresVerification:
return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
case SignInStatus.Failure:
default:
ModelState.AddModelError("", language == "Invalid login attempt.");
return View(model);
}
}
I try also to change applicationdbcontext create in a startup file
app.CreatePerOwinContext(() => ApplicationDbContext.Create(ConString.dbCatlogConn));
but the ApplicationSignInManager always uses the main connection - it doesn't update for the new one after login despite I got the connection string correctly
