我知道这是重复的问题,即使在这里也是如此,但是我已经毫无意义地尝试了所有解决方案。我有主数据库,用户在该数据库中检查是否找到了该数据库,并由自己的数据库或相关数据库返回,该数据库应将连接字符串更改为当前使用的字符串。我的问题是,尽管数据库正确无误,但dbcontext
可以在主数据库上运行,但又再次转移了我的权限。
我的applicationdbcontext
是
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);
}
这是我的公开课
public class ConString
{
public static string dbCatlogConn { get; set; }
}
这是我在accountcontroller类中的登录名
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);
}
}
我也尝试更改在启动文件中创建的applicationdbcontext
app.CreatePerOwinContext(() => ApplicationDbContext.Create(ConString.dbCatlogConn));
但是ApplicationSignInmanager
始终使用主连接-即使我正确获得了连接字符串,登录后它也不会为新连接更新