ApplicationDbContext 的 DefaultConnection 字符串出现随机且不一致的错误

Random and inconsistent error with the DefaultConnection string for ApplicationDbContext

我的 MVC 应用程序中有一个奇怪的行为。在某些时候,我开始在所有页面上收到一个奇怪的 Format of the initialization string does not conform to specification starting at index 0. 错误。

我说它是随机且不一致的,因为:

  1. 当没有对任何连接字符串进行任何更改时,它开始发生
  2. 大多数时候我在 Firefox 上遇到错误
  3. 同时适用于Chrome和IE

现在,我的发布配置文件添加了有问题的连接字符串,它看起来像这样:

<add name="DefaultConnection" connectionString="DefaultConnection_ConnectionString" providerName="System.Data.SqlClient" />

当我删除此连接字符串时,Firefox 中的错误变为

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

(同样,它同时适用于 Chrome 和 IE)。

'it works' 我的意思是我可以使用我的域凭据登录页面(它应该使用 AD 身份验证)并浏览页面等。

我的 MVC 应用程序只应该直接使用 EF 进行审计跟踪日志记录(可选,存储在本地数据库中)。 为此,我设置了一个单独的 DbContext class 指向一个特定的单独连接字符串 - 这一切都很好。

'DefaultConnection' 由样板 OWIN 身份验证代码使用的 ApplicationDbContext 使用:

public ApplicationDbContext() : base("DefaultConnection", throwIfV1Schema: false)

业务数据访问层被分离到不同的网络中API,我在那里设置了正确的 DbContext 和连接字符串(也工作正常) 我的 Web 应用程序的身份验证也在单独的 Web API 中完成。我将 HTTPS post 中的凭据传递给 API,使用 PrincipalContext 和 return 组声明验证它们返回到 Web 应用程序。

在那里,我得到了一个 IAuthenticationManager 的实例 this.HttpContext.GetOwinContext().Authentication

并使用我的 ClaimsIdentity 实例登录

this.AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = rememberMe }, identity);

现在,有两个问题:

  1. 我的应用程序似乎根本不需要 ApplicationDbContext 和使用它的位?我是说这些:

app.CreatePerOwinContext(ApplicationDbContext.Create); app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create); app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);

  1. 为什么它开始抛出错误,为什么它在其他浏览器中仍然有效??

非常感谢您的建议!

============================

更新 实际上,现在我在 Chrome 中也遇到了同样的错误,这让我认为这一定与我在该浏览器上登录该页面的时间长短有关......或者其他......

所以,清除 cookie 后,我的页面在给定的浏览器中再次运行。什么……为什么?

嗯,现在我知道了。

原因是样板 ConfigureAuth() 方法中的 'OnValidateIdentity' 位代码。

app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                LoginPath = new PathString("/Account/Login"),
                Provider = new CookieAuthenticationProvider
                {
                    // Enables the application to validate the security stamp when the user logs in.
                    // This is a security feature which is used when you change a password or add an external login to your account.  
                    OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                        validateInterval: TimeSpan.FromMinutes(30),
                        regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
                },


            });

它试图在指定的时间间隔(30 分钟)后调用身份验证。因此,在我登录 30 分钟后,它尝试验证身份并因此调用了有问题的 DbContext。

它 'sometimes worked' 以及我称它为 random/inconsistent 的原因是我时不时会清除 cookie 或尝试不同的浏览器,这些浏览器不包含触发验证的 cookie。然后,我会看到它工作,并重复工作相当长一段时间