自定义ClaimsIdentity导致实体框架中的异常

时间:2014-09-03 17:53:55

标签: c# .net entity-framework claims-based-identity

  1. 在管道的早期,使用自定义AuthorizationAttribute,我们使用扩展的ClaimsIdentity设置Thread.CurrentPrincipal

        CustomIdentity identity = new CustomIdentity("Bearer");
        identity.AddClaim(new Claim("customClaim", customClaimValue));
        ...
        ClaimsPrincipal principal = new ClaimsPrincipal(identity);
        Thread.CurrentPrincipal = principal;
    
  2. 稍后在管道中我们尝试使用EF6

    从我们的SQL Server 2012数据库中获取值
        var dataList = _repository.GetAll().FirstOrDefault(x => x.Col1 == parameter);
    
  3. 然后我们在运行时获得异常。外部例外是:
  4.   

    System.Data.Entity.Core.ProviderIncompatibleException:提供者   没有返回ProviderManifest实例

    ,内部例外是:

      

    无法找到程序集' OurApp.Authorization.Models,Version = 1.0.0.0,   Culture = neutral,PublicKeyToken = null'

    其中OurApp.Authorization.Models是CustomIdentity所在的命名空间和项目名称:

    namespace OurApp.Authorization.Models
    {
    [Serializable]
    public class CustomIdentity : ClaimsIdentity
    {
            ...
    }
    }
    

    奇怪的是,如果我在转到数据存储之前立即将Thread.CurrentPrincipal设置为当前的WindowsIdentity,那么代码工作正常:

    Thread.CurrentPrincipal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
    var dataList = _repository.GetAll().FirstOrDefault(x => x.Col1 == parameter);
    

    似乎EntityFramework需要了解我们的CustomIdentity,但我不知道为什么。无论如何,我在数据访问项目中添加了引用,但它似乎没有影响。

    我们确保我们没有使用Windows身份验证来访问SQL Server,并且我们所有的连接字符串现在都不使用集成安全性,但它仍然无法正常工作。

    有没有人知道为什么在EntityFramework中设置自定义ClaimsIdentity会导致此异常?

    以下是所要求的完整例外情况:

      

    System.Data.Entity.Core.ProviderIncompatibleException未处理   用户代码HResult = -2146233087 Message =提供者未返回   ProviderManifest实例。 Source = EntityFramework StackTrace:          在System.Data.Entity.Core.Common.DbProviderServices.GetProviderManifest(String   manifestToken)          在System.Data.Entity.Utilities.DbConnectionExtensions.GetProviderInfo(DbConnection)   连接,DbProviderManifest& providerManifest)          在System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)          在System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext)   internalContext)          在System.Data.Entity.Internal.RetryLazy 2.GetValue(TInput input) at System.Data.Entity.Internal.LazyInternalContext.InitializeContext() at System.Data.Entity.Internal.InternalContext.Initialize() at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) at System.Data.Entity.Internal.Linq.InternalSet 1.Initialize()          在System.Data.Entity.Internal.Linq.InternalSet 1.get_InternalContext() at System.Data.Entity.Infrastructure.DbQuery 1.System.Linq.IQueryable.get_Provider()          在System.Linq.Queryable.FirstOrDefault [TSource](IQueryable 1 source, Expression 1谓词)          在OurApp.Module.ViewStore.EntityFramework.InformationViewStore.Get(String   userId)in   C:\代码\ OurApp \ OurApp.Module.ViewStore \的EntityFramework \ InformationViewStore.cs:行   156          位于c:\ Code \ OurApp \ OurApp.Module.Azure.Api \ Hubs \ ModuleHub.cs中的OurApp.Module.Azure.Api.Hubs.OurAppHub.OnConnected():第119行          在Microsoft.AspNet.SignalR.Hubs.HubDispatcher.Connect(IHub集线器)          在Microsoft.AspNet.SignalR.Hubs.HubPipelineModule。<> c__DisplayClass9.b__7(IHub)   毂)          在Microsoft.AspNet.SignalR.Hubs.HubPipelineModule。<> c__DisplayClass9.b__7(IHub)   毂)          在Microsoft.AspNet.SignalR.Hubs.HubPipeline.Connect(IHub集线器)          在Microsoft.AspNet.SignalR.Hubs.HubDispatcher.b__1c(IHub   毂)          在Microsoft.AspNet.SignalR.Hubs.HubDispatcher。<> c__DisplayClass33.b__2f(IHub)   实例)          at System.Linq.Enumerable.WhereSelectListIterator 2.MoveNext() at System.Linq.Buffer 1..ctor(IEnumerable 1 source) at System.Linq.Enumerable.ToArray[TSource](IEnumerable 1 source)          在Microsoft.AspNet.SignalR.Hubs.HubDispatcher.ExecuteHubEvent(IRequest)   request,String connectionId,Func 2 action) at Microsoft.AspNet.SignalR.Hubs.HubDispatcher.OnConnected(IRequest request, String connectionId) at Microsoft.AspNet.SignalR.PersistentConnection.ProcessStartRequest(HostContext context, String connectionId) at Microsoft.AspNet.SignalR.PersistentConnection.ProcessRequest(HostContext context) at Microsoft.AspNet.SignalR.Hubs.HubDispatcher.ProcessRequest(HostContext context) at Microsoft.AspNet.SignalR.PersistentConnection.ProcessRequest(IDictionary 2   环境)          在Microsoft.AspNet.SignalR.Owin.Middleware.HubDispatcherMiddleware.Invoke(IOwinContext   上下文)          在Microsoft.Owin.Mapping.MapMiddleware.d__0.MoveNext()InnerException:System.Runtime.Serialization.SerializationException          的HResult = -2146233076          消息=无法找到程序集' OurApp.Authorization.Models,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null'。          来源= mscorlib程序          堆栈跟踪:               在System.Runtime.Serialization.Formatters.Binary.BinaryAssemblyInfo.GetAssembly()               在System.Runtime.Serialization.Formatters.Binary.ObjectReader.GetType(BinaryAssemblyInfo)   assemblyInfo,String name)               在System.Runtime.Serialization.Formatters.Binary.ObjectMap..ctor(String   objectName,String [] memberNames,BinaryTypeEnum [] binaryTypeEnumA,   Object [] typeInformationA,Int32 [] memberAssemIds,ObjectReader   objectReader,Int32 objectId,BinaryAssemblyInfo assemblyInfo,   SizedArray assemIdToAssemblyTable)               在System.Runtime.Serialization.Formatters.Binary .__ BinaryParser.ReadObjectWithMapTyped(BinaryObjectWithMapTyped)   记录)               在System.Runtime.Serialization.Formatters.Binary .__ BinaryParser.Run()               在System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler)   handler,__BinaryParser serParser,Boolean fCheck,Boolean   isCrossAppDomain,IMethodCallMessage methodCallMessage)               在System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream   serializationStream,HeaderHandler handler,Boolean fCheck,Boolean   isCrossAppDomain,IMethodCallMessage methodCallMessage)               在System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream   serializationStream,HeaderHandler handler,Boolean fCheck,   IMethodCallMessage methodCallMessage)               在System.Security.Claims.ClaimsPrincipal.DeserializeIdentities(String   身份)               在System.Security.Claims.ClaimsPrincipal.OnDeserializedMethod(StreamingContext)   上下文)               在System.AppDomain.get_Evidence()               在System.AppDomain.get_Evidence()               在System.Configuration.ClientConfigPaths.GetEvidenceInfo(AppDomain   appDomain,String exePath,String&的typeName)               在System.Configuration.ClientConfigPaths.GetTypeAndHashSuffix(AppDomain   appDomain,String exePath)               在System.Configuration.ClientConfigPaths..ctor(String exePath,Boolean includeUserConfig)               在System.Configuration.ClientConfigPaths.GetPaths(String exePath,Boolean includeUserConfig)               在System.Configuration.ClientConfigurationHost.get_ConfigPaths()               在System.Configuration.ClientConfigurationHost.RequireCompleteInit(IInternalConfigRecord)   记录)               在System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String   configKey,Boolean getLkg,Boolean checkPermission,Boolean   getRuntimeObject,Boolean requestIsHere,Object&结果,对象&   resultRuntimeObject)               在System.Configuration.BaseConfigurationRecord.GetSection(String   configKey,Boolean getLkg,Boolean checkPermission)               在System.Configuration.BaseConfigurationRecord.GetSection(String   configKey)               at System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(String   sectionName)               在System.Configuration.ConfigurationManager.GetSection(String   sectionName)               在System.Xml.XmlConfiguration.XmlReaderSection.get_ProhibitDefaultUrlResolver()               在System.Xml.XmlConfiguration.XmlReaderSection.CreateDefaultResolver()               在System.Xml.Schema.Parser..ctor(SchemaType schemaType,XmlNameTable nameTable,SchemaNames schemaNames,   ValidationEventHandler eventHandler)               在System.Xml.Schema.XmlSchema.Read(XmlReader reader,ValidationEventHandler validationEventHandler)               在System.Xml.Schema.XmlSchema.Read(Stream stream,ValidationEventHandler validationEventHandler)               在System.Data.Entity.Core.SchemaObjectModel.Schema.SomSchemaSetHelper.AddXmlSchemaToSet(XmlSchemaSet)   schemaSet,XmlSchemaResource schemaResource,HashSet 1 schemasAlreadyAdded) at System.Data.Entity.Core.SchemaObjectModel.Schema.SomSchemaSetHelper.ComputeSchemaSet(SchemaDataModelOption dataModel) at System.Data.Entity.Core.Common.Utils.Memoizer 2.<> c__DisplayClass2.b__0()               在System.Data.Entity.Core.Common.Utils.Memoizer 2.Result.GetValue() at System.Data.Entity.Core.Common.Utils.Memoizer 2.评估(TArg arg)               在System.Data.Entity.Core.SchemaObjectModel.Schema.SomSchemaSetHelper.GetSchemaSet(SchemaDataModelOption)   数据模型)               在System.Data.Entity.Core.SchemaObjectModel.Schema.CreateXmlReaderSettings()               在System.Data.Entity.Core.SchemaObjectModel.Schema.Parse(XmlReader   sourceReader,String sourceLocation)               在System.Data.Entity.Core.SchemaObjectModel.SchemaManager.ParseAndValidate(IEnumerable 1 xmlReaders, IEnumerable 1 sourceFilePaths,SchemaDataModelOption   dataModel,AttributeValueNotification providerNotification,   AttributeValueNotification providerManifestTokenNotification,   ProviderManifestNeeded providerManifestNeeded,IList 1& schemaCollection) at System.Data.Entity.Core.SchemaObjectModel.SchemaManager.ParseAndValidate(IEnumerable 1   xmlReaders,IEnumerable 1 sourceFilePaths, SchemaDataModelOption dataModel, DbProviderManifest providerManifest, IList 1&   schemaCollection)               在System.Data.Entity.Core.SchemaObjectModel.SchemaManager.LoadProviderManifest(XmlReader)   xmlReader,String location,Boolean checkForSystemNamespace,Schema&   架构)               在System.Data.Entity.Core.Common.DbXmlEnabledProviderManifest.Load(XmlReader)   读者)               在System.Data.Entity.Core.Common.DbXmlEnabledProviderManifest..ctor(XmlReader)   读者)               在System.Data.Entity.SqlServer.SqlProviderManifest..ctor(String   manifestToken)               在System.Data.Entity.SqlServer.SqlProviderServices.b__f(String   S)               在System.Collections.Concurrent.ConcurrentDictionary 2.GetOrAdd(TKey key, Func 2 valueFactory)               at System.Data.Entity.SqlServer.SqlProviderServices.GetDbProviderManifest(String   versionHint)               在System.Data.Entity.Core.Common.DbProviderServices.GetProviderManifest(String   manifestToken)          的InnerException:

0 个答案:

没有答案