当我在ClaimsIdentity中对某些声明进行'foreach'循环时,我遇到了一个问题。 当我逐步完成它 - 它应该完成枚举,我看到它回到'in'然后在我得到关于连接到SQL Server的HttpException之前得到十秒钟的延迟。 我知道这听起来很奇怪,但是当它根据调试发生时 - 在我的FindAllClaimsByType方法的foreach循环中(我使用Entity Framework后很快连接到数据库,但我的断点从未受到影响)。 编辑:我应该提一下,如果我跳过这个并在控制器中执行某些操作,我可以很好地连接到我的数据库。这是在ASP.NET MVC 4
中 编辑2:我把它缩小了一点。只有在找不到Claim
时才会这样。我如何枚举声明(如果我使用ClaimsPrincipal.HasClaim()
或foreach
循环或.Any()
LINQ表达式)并不重要 - 如果可以,它将始终抛出此异常找到索赔。
建立与SQL Server的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确,以及SQL Server是否配置为允许远程连接。 (提供程序:SQL网络接口,错误:26 - 查找指定的服务器/实例时出错)
我的代码如下:
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):建立与SQL Server的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确,以及SQL Server是否配置为允许远程连接。 (提供程序:SQL网络接口,错误:26 - 找到指定的服务器/实例时出错)] 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&amp; connection)+5313314 System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection,DbConnectionFactory connectionFactory,TaskCompletionSource1 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
答案 0 :(得分:11)
我在Azure云解决方案中遇到了全新的ASP MVC Web角色问题。尚未设置SQL,除了Azure ACS联合登录之外,还没有其他任何功能。我只是在测试ACS,WIF和多个身份提供商的功能。
默认情况下,RoleManager功能似乎已启动。一旦我在web.config中删除它 - 在枚举声明时不再尝试SQL Server访问。
添加以下行:
<modules>
<remove name="RoleManager" />
...
希望这有帮助。
Rick Shuri