SimpleMembership、MVC4、AuthorizeAttribute 和角色
我正在嘗試向我的一些 MVC4 控制器添加授權屬性,只要它是一個計劃
[Authorize]或[Authorize(Users="myuser")],它們就可以正常工作,但是當我添加任何類型的角色過濾時,它就會分崩離析,例如。[Authorize(Roles="admin")]. 然後我開始收到如下錯誤:“/”應用程序中的伺服器錯誤。
建立與 SQL Server 的連接時發生與網路相關或特定於實例的錯誤。伺服器未找到或無法訪問。驗證實例名稱是否正確以及 SQL Server 是否配置為允許遠端連接。(提供者:SQL 網路介面,錯誤:26 - 錯誤定位伺服器/指定的實例)
說明:執行目前 Web 請求期間發生未處理的異常。請查看堆棧跟踪以獲取有關錯誤及其源自程式碼的位置的更多資訊。
SQLExpress 數據庫文件自動創建錯誤:
連接字元串使用應用程序 App_Data 目錄中的數據庫位置指定本地 Sql Server Express 實例。提供者試圖自動創建應用程序服務數據庫,因為提供者確定該數據庫不存在。成功檢查應用程序服務數據庫是否存在並自動創建應用程序服務數據庫需要以下配置要求:
如果應用程序在 Windows 7 或 Windows Server 2008R2 上執行,則需要特殊的配置步驟來啟用提供程序數據庫的自動創建。其他資訊可在以下網址獲得:http: //go.microsoft.com/fwlink/ ?LinkId=160102. 如果應用程序的 App_Data 目錄尚不存在,則 Web 伺服器帳戶必須具有對應用程序目錄的讀寫權限。這是必要的,因為如果 App_Data 目錄不存在,Web 伺服器帳戶將自動創建它。如果應用程序的 App_Data 目錄已經存在,則 Web 伺服器帳戶只需要對應用程序的 App_Data 目錄具有讀寫權限。這是必要的,因為 Web 伺服器帳戶將嘗試驗證 Sql Server Express 數據庫是否已存在於應用程序的 App_Data 目錄中。從 Web 伺服器帳戶撤消對 App_Data 目錄的讀取訪問權限將阻止提供程序正確確定 Sql Server Express 數據庫是否已存在。當提供程序嘗試創建現有數據庫的副本時,這將導致錯誤。需要寫入權限,因為在創建新數據庫時會使用 Web 伺服器帳戶的憑據。機器上必須安裝 Sql Server Express。Web 伺服器帳戶的程序標識必須具有本地使用者配置文件。有關如何為電腦和域帳戶創建本地使用者配置文件的詳細資訊,請參閱自述文件。
源錯誤:
在執行目前 Web 請求期間生成了未處理的異常。可以使用下面的異常堆棧跟踪來辨識有關異常起源和位置的資訊。
堆棧跟踪:
$$ SqlException (0x80131904): 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) $$
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException 異常,布爾 breakConnection,Action 1 重試,DbConnectionOptions userOptions,DbConnectionInternal& 連接)+5311874 System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection,DbConnectionFactory connectionFactory,TaskCompletionSource 1 重試)+83 System.Data.SqlClient.SqlConnection.Open() +96 System.Web.Management.SqlServices.GetSqlConnection(String server, String user, String password, Boolean trust, String connectionString) +76`1 wrapCloseInAction) +5295167 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +242
System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, Boolean withFailover) +5307115
System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, Boolean withFailover) +145
System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout) +920
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) +143System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`
$$ HttpException (0x80004005): Unable to connect to SQL Server database. $$
System.Web.Management.SqlServices.GetSqlConnection(String server, String user, String password, Boolean trust, String connectionString) +131
System.Web.Management.SqlServices.SetupApplicationServices(String server, String user, String password, Boolean trust, 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,字元串數據目錄,字元串連接字元串)+386 版本資訊:Microsoft .NET Framework 版本:4.0.30319;ASP.NET 版本:4.0.30319.17929
我真的不確定這裡發生了什麼。同樣,如果我刪除了授權屬性的角色位,那麼它可以正常工作(至少可以阻止未經授權的使用者),但是如果我必須將管理員使用者硬編碼到應用程序中,那就不好了!
知道這裡發生了什麼,以及如何使其正常工作嗎?
謝謝,
我找到了解決方案(儘管我確信有更好的方法)。首先,SimpleMembership 使用的數據庫沒有足夠早地初始化,所以我移動了這一行:
WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);到 Global.asax 文件並確保它儘早執行。這行程式碼可以在 Filters -> InitializeSimpleMembershipAttribute.cs 中找到,大約第 41 行
編輯:看來這下一點是沒有必要的……
其次,該
[InitializeSimpleMembership]屬性需要添加到成員資格可能成為問題的任何類中——對我來說就是所有這些,所以我添加了以下行:filters.Add(new InitializeSimpleMembershipAttribute());到 app_start 文件夾中的 FilterConfig.cs 文件。
這兩個簡單的更改似乎已經解決了它。我確信我可以對 MVC4 中包含的 SimpleMembership 的一般工作進行一些改進——它似乎寫得不太好(硬編碼的連接字元串等!),所以這些問題可能是通過首先編寫好 SimpleMembership 內容來修復!