Identity Role 检查在本地工作,但在部署的实例上不工作 - Entity Framework with Owin

Identity Role check works locally, but not on deployed instances - Entity Framework with Owin

我遇到了这个错误:

Server Error in '/' Application.
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)    
Description: An unhandled exception occurred during the execution of the current web req    uest. Please review the stack trace     for more information about the error and where it originated in the code.    

SQLExpress database file auto-creation error:

The connection string specifies a local Sql Server Express instance using a database location within the application's     App_Data directory. The provider attempted to automatically create the application services database because the provid    er     determined that the database does not exist. The following configuration requirements are necessary to successfully che    ck for     existence of the application services database and automatically create the application services database:    

If the application is running on either Windows 7 or Windows Server 2008R2, special configuration steps are necessary to     enable automatic creation of the provider database. Additional information is available at: http://go.microsoft.    com/fwlink/?LinkId=160102. If the application's App_Data directory does not already exist, the web server accoun    t must have     read and write access to the application's directory. This is necessary because the web server account will auto    matically     create the App_Data directory if it does not already exist.    
If the application's App_Data directory already exists, the     web server account only requires read and write access to the     application's App_Data directory. This is necessary because     the web server account will attempt to verify that the Sql Ser    ver     Express database already exists within the application's Ap    p_Data directory. Revoking read access on the App_Data director    y     from the web server account will prevent the provider from     correctly determining if the Sql Server Express database alread    y     exists. This will cause an error when the provider attempts     to create a duplicate of an already existing database. Write     access is required because the web server account's credent    ials are used when creating the new database.    
Sql Server Express must be installed on the machine.    
The process identity for the web server account must have a local user profile. See the readme document for details on how to     create a local user profile for both machine and domain accounts.

当我在用户上调用此扩展方法时抛出此异常:

public static bool IsInAnyRole(this IPrincipal principal, params string[] roles)
{
    return roles.Any(principal.IsInRole);
}

事实上,这个扩展方法在我的本地机器上运行良好。仅在部署(远程服务器和 Azure - 我都尝试过)时失败。

异常似乎指定它无法连接到 SQL Express,但我根本不应该使用 SQL Express。我唯一的连接字符串指向 SQL 服务器。

我试过远程服务器的 SQL 数据库和 Azure SQL 实例;抛出相同的异常。

我还尝试确保所有相关参考文献都是 Copy Local = true。

我和我的伙伴们完全被难住了。有任何想法吗?

更新:

我相信我已经追踪到 Entity Framework 在某些时候创建并依赖 LocalDB 实例这一事实,尽管我的配置中或任何地方都找不到这样的实例一个配置。有什么地方可以自动完成吗?

嗯!想通了。

这个问题专门与角色管理器有关。此功能通过以下 web.config 行启用:

<roleManager enabled="true" />

但是 - 上面的声明本身并没有声明连接字符串。人们会认为它默认为 web.config 顶部的任何 DefaultConnection 字符串,但事实并非如此 - 它使用 machine.config 中的任何内容。就我而言,那是 LocalDB/SQL Express。

为了修复它,我做了两件事:

安装了 Microsoft AspNet Providers,这是一个 NuGet 包,它获取所有不同的身份提供者(用户、角色等)并均衡它们的配置。

Install-Package Microsoft.AspNet.Providers

然后,我确保 roleManager 节点已完全声明:

<roleManager enabled="true" defaultProvider="DefaultRoleProvider">
  <providers>
    <clear />
    <add name="DefaultRoleProvider" type="System.Web.Providers.DefaultRoleProvider, System.Web.Providers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/" />
  </providers>
</roleManager>

现在一切正常!