5

当我对 ClaimsIdentity 中的某些声明执行“foreach”循环时遇到问题。当我逐步完成它时 - 在它应该完成枚举的时候,我看到它回到“in”,然后在我得到一个关于连接到 SQL Server 的 HttpException 之前得到十秒的延迟。我知道这听起来很奇怪,但那是根据调试发生的 - 在我的 FindAllClaimsByType 方法的 foreach 循环中(我确实在使用实体框架后不久连接到数据库,但我的断点从未被击中)。编辑:我应该提到,如果我跳过这个并在控制器中做一些事情,我可以很好地连接到我的数据库。这是在 ASP.NET MVC 4

编辑2:我已经缩小了一点。只有在找不到Claim. 我如何枚举声明并不重要(如果我使用循环或ClaimsPrincipal.HasClaim()LINQ表达式) - 如果找不到声明,它总是会抛出此异常。foreach.Any()

建立与 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 异常,布尔 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, TaskCompletionSource1 retry,DbConnectionOptions userOptions,DbConnectionInternal& 连接)+5313314 System.Data .ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions) +143 System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource1 次重试) +83 System.Data.SqlClient.SqlConnection.Open() +96 System.Web.Management.SqlServices.GetSqlConnection(String server, String user, String password, Boolean trust, String connectionString) +76

[HttpException (0x80004005): 无法连接到 SQL Server 数据库。] System.Web.Management.SqlServices.GetSqlConnection(String server, String user, String password, Boolean trust, String connectionString) +131 System.Web.Management.SqlServices。 SetupApplicationServices(字符串服务器,字符串用户,字符串密码,布尔信任,字符串连接字符串,字符串数据库,字符串 dbFileName,SqlFeatures 功能,布尔安装)+89 System.Web.Management.SqlServices.Install(字符串数据库,字符串 dbFileName,字符串连接字符串) +27 System.Web.DataAccess.SqlConnectionHelper.CreateMdfFile(字符串 fullFileName,字符串 dataDir,字符串 connectionString)+386

4

1 回答 1

12

我在 Azure 云解决方案中使用全新的 ASP MVC Web 角色时遇到了同样的问题。尚未设置 SQL,也没有其他功能,除了 Azure ACS 联合登录尚未实现。我只是在测试 ACS、WIF 和多个身份提供者的功能。

似乎 RoleManager 功能是默认内置的。一旦我在 web.config 中删除它 - 在枚举声明时不再尝试访问 SQL Server。

添加这一行:

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

希望这会有所帮助。

里克·舒里

于 2013-05-01T16:03:50.100 回答