2013-03-16 111 views
4

當我在ClaimsIdentity中的某些聲明中進行'foreach'循環時,遇到問題。 當我逐步完成它時 - 它應該完成枚舉,我看到它回到'in'中,然後在得到關於連接到SQL Server的HttpException之前得到10秒的延遲。 我知道這聽起來很奇怪,但那是根據調試發生的時候 - 在FindAllClaimsByType方法的foreach循環中(我在使用Entity Framework之後很快連接到數據庫,但是我的斷點從來沒有碰到過)。 編輯:我應該提到我可以連接到我的數據庫罰款,如果我跳過這一點,並在控制器做一些事情。這是在ASP.NET MVC 4通過聲明枚舉導致HttpException SqlException

編輯2:我已經縮小了一點。只有當它找不到Claim。不管我如何通過索賠列舉(如果我使用ClaimsPrincipal.HasClaim()foreach循環或.Any() LINQ表達式) - 如果它找不到索賠,它總是會拋出此異常。

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)

我的代碼如下:

public class ClaimsService 
{ 

    private ClaimsIdentity _identity; 

    public ClaimsService(ClaimsIdentity identity) 
    { 
     _identity = identity; 
    } 

    private Claim FindFirstClaimByType(string claimtype) 
    { 
     return FindAllClaimsByType(claimtype).FirstOrDefault(); 
    } 

    private IEnumerable<Claim> FindAllClaimsByType(string claimtype) 
    { 
     ICollection<Claim> claims = new Collection<Claim>(); 
     foreach (Claim claim in _identity.Claims) 
     { 
      if (claim.Type == claimtype) 
      { 
       claims.Add(claim); 
      } 
     } 

     return claims; 
    } 

    public School FindSchoolFromClaims() 
    { 
     string realm = FindFirstClaimByType(Data.Configuration.RealmClaimType).Value.ToString(); 

     using (SqlDatabaseContext db = new SqlDatabaseContext()) 
     { 
      School school = null; 


      school = db.Schools.Where(s => s.Realm == realm).FirstOrDefault(); 

      return school; 
     } 
    } 

    public Developer FindDeveloperFromClaims() 
    { 
     string realm = FindFirstClaimByType(Data.Configuration.RealmClaimType).Value.ToString(); 
     string nameidentifier = FindFirstClaimByType(ClaimTypes.NameIdentifier).Value.ToString(); 

     using (SqlDatabaseContext db = new SqlDatabaseContext()) 
     { 
      Developer developer = null; 


      if (realm == Data.Configuration.SoloDeveloperRealmMsft) 
      { 
       developer = db.Developers.OfType<SoloDeveloper>().Where(dev => dev.NameIdentifier == nameidentifier).FirstOrDefault(); 
      } 
      else 
      { 
       developer = db.Developers.OfType<CompanyDeveloper>().Where(dev => dev.Realm == realm).FirstOrDefault(); 
      } 

      return developer; 
     } 
    } 
} 

堆棧跟蹤:

[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 exception, Boolean breakConnection, Action 1 wrapCloseInAction) +5296071 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +558 System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, Boolean withFailover) +5308555 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) +5311099 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& connection) +5313314 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): Unable to connect to SQL Server database.] 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

+0

打開SQL分析器。如果循環枚舉,那麼在循環結束時它是幹什麼的?什麼? – 2013-03-16 13:10:04

+0

連接字符串中的用戶名和密碼不正確,或者您嘗試連接的網絡出現問題 – dakait 2013-03-16 14:35:49

+0

@soadyp使用SQL Profiler嗎?目前我正在使用LocalDB,但我可以切換到SQL Azure。 @dakait連接字符串是好的 - 如果我不使用'ClaimsService',那麼我可以訪問我的數據庫很好 – 2013-03-16 23:38:15

回答

11

我有在Azure雲解決方案全新的ASP MVC Web角色相同的問題。尚未設置SQL,除Azure ACS聯合登錄之外,還沒有其他功能實現。我只是測試ACS,WIF和多個身份提供者的功能。

默認情況下,RoleManager功能似乎是烘焙的。一旦我在web.config中將其刪除 - 枚舉聲明時不再嘗試訪問SQL Server。

加入這一行:

<modules> 
    <remove name="RoleManager" /> 
    ... 

希望幫助。

Rick Shuri

+0

完美,在將EntityFramework nuget添加到組織登錄模板後遇到同樣的問題。 – angularsen 2015-10-07 13:07:10