为什么我的MVC 4授权检查会抛出异常?

时间:2013-01-14 08:41:47

标签: asp.net-mvc asp.net-mvc-4 forms-authentication

我正在使用Visual Studio 2012和SQL Server Express 2012.我使用“Internet应用程序”模板创建了一个MVC 4 Web应用程序。

我想在我的SQL Server 2012 Express实例上使用现有数据库(而不是模板的MDF)。我修改了我的web.config以指向我的本地SQL express实例,并删除了模板生成的MDF。我的Web.Config有:

<add name="DefaultConnection" 
    connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=Administration;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/>

股票会员代码有一个初始化DB的调用:

WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);

一旦应用程序启动,就会像我期望的那样在管理数据库中创建表。我可以创建用户,并在管理中填充它们。我可以填充角色表并通过代码向用户的配置文件添加角色并验证它是否花了。 e.g:

Roles.GetRolesForUser("kristian"); // = string[] { "Administrator" }

但是,如果我尝试使用基于角色的授权,例如:

[Authorize(Roles = "Administrator")]

我得到以下异常:

Failed to generate a user instance of SQL Server due to a failure in starting the process for the user instance. The connection will be closed.

它提供了一个冗长讨厌的堆栈跟踪,这让我觉得它仍然在尝试创建一个MDF文件:

[SqlException(0x80131904):由于启动用户实例的进程失败,无法生成SQL Server的用户实例。连接将被关闭。]    System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception,Boolean breakConnection,Action 1 wrapCloseInAction) +5295167 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +242 System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) +1682 System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +69 System.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(Boolean enlistOK) +30 System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, Boolean withFailover) +317 System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout) +889 System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance) +307 System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions) +434 System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions) +5309659 System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup, DbConnectionOptions userOptions) +38 System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource 1 retry,DbConnectionOptions userOptions,DbConnectionInternal&amp; connection)+5311874    System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection,DbConnectionFactory connectionFactory,TaskCompletionSource 1 retry, DbConnectionOptions userOptions) +143 System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource 1 retry)+83    System.Data.SqlClient.SqlConnection.Open()+96    System.Web.Management.SqlServices.GetSqlConnection(String server,String user,String password,Boolean trusted,String connectionString)+76

[HttpException(0x80004005):无法连接到SQL Server数据库。]    System.Web.Management.SqlServices.GetSqlConnection(String server,String user,String password,Boolean trusted,String connectionString)+131    System.Web.Management.SqlServices.SetupApplicationServices(String server,String user,String password,Boolean trusted,String connectionString,String database,String dbFileName,SqlFeatures features,Boolean install)+89    System.Web.Management.SqlServices.Install(String database,String dbFileName,String connectionString)+27    System.Web.DataAccess.SqlConnectionHelper.CreateMdfFile(String fullFileName,String dataDir,String connectionString)+386

这一束快乐底部的一行让我觉得它与现在删除的MDF('System.Web.DataAccess.SqlConnectionHelper.CreateMdfFile')有关。我这是对的吗?或者模板实施的授权是否不完整?

如果其他人已经处理过此问题或知道我的属性使用失败的原因,我将非常感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

我有同样的问题。它似乎与不正确初始化SimpleMembership有关。请确保使用[InitializeSimpleMembership]或直接致电Database.SetInitializer<UsersContext>(null);