多数据库登录

问题描述 投票:0回答:1

我知道这是重复的问题,即使在这里,但我毫无道理地尝试了所有解决方案。我有主数据库,用户在该数据库中检查是否找到了该数据库,并由自己的数据库或相关数据库返回,该数据库应将连接字符串更改为当前使用的字符串。我的问题是,尽管数据库正确无误,但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始终使用主连接-尽管我正确获得了连接字符串,登录后它也不会为新连接更新

c# asp.net-mvc code-first
1个回答
-1
投票
© www.soinside.com 2019 - 2024. All rights reserved.