1
  1. Early in the pipeline, using a custom AuthorizationAttribute we are setting Thread.CurrentPrincipal using an extended ClaimsIdentity

        CustomIdentity identity = new CustomIdentity("Bearer");
        identity.AddClaim(new Claim("customClaim", customClaimValue));
        ...
        ClaimsPrincipal principal = new ClaimsPrincipal(identity);
        Thread.CurrentPrincipal = principal;
    
  2. Later in the pipeline we try to get values from our SQL Server 2012 database using EF6

        var dataList = _repository.GetAll().FirstOrDefault(x => x.Col1 == parameter);
    
  3. We then get an exception at runtime. The outer exception is:

System.Data.Entity.Core.ProviderIncompatibleException : The provider did not return a ProviderManifest instance

and the inner exception is:

Unable to find assembly 'OurApp.Authorization.Models, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'

where OurApp.Authorization.Models is the namespace and project name where CustomIdentity lives:

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

The strange thing is that if I set the Thread.CurrentPrincipal to the current WindowsIdentity immediately before going to the data store then the code works fine:

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

It seems that EntityFramework needs to know about our CustomIdentity but I have no idea why. I added references in the data access project anyway but it didn't seem to impact.

We made sure that we weren't using WindowsAuthentication to access SQL Server and all our connectionstrings do not use integrated security now but it still doesn't work.

Does anyone know why setting a custom ClaimsIdentity is causing this exception in EntityFramework?

Here's the full exception as requested:

System.Data.Entity.Core.ProviderIncompatibleException was unhandled by user code HResult=-2146233087 Message=The provider did not return a ProviderManifest instance. Source=EntityFramework StackTrace: at System.Data.Entity.Core.Common.DbProviderServices.GetProviderManifest(String manifestToken) at System.Data.Entity.Utilities.DbConnectionExtensions.GetProviderInfo(DbConnection connection, DbProviderManifest& providerManifest) at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection) at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext) at System.Data.Entity.Internal.RetryLazy2.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.InternalSet1.Initialize() at System.Data.Entity.Internal.Linq.InternalSet1.get_InternalContext() at System.Data.Entity.Infrastructure.DbQuery1.System.Linq.IQueryable.get_Provider() at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable1 source, Expression1 predicate) at OurApp.Module.ViewStore.EntityFramework.InformationViewStore.Get(String userId) in c:\Code\OurApp\OurApp.Module.ViewStore\EntityFramework\InformationViewStore.cs:line 156 at OurApp.Module.Azure.Api.Hubs.OurAppHub.OnConnected() in c:\Code\OurApp\OurApp.Module.Azure.Api\Hubs\ModuleHub.cs:line 119 at Microsoft.AspNet.SignalR.Hubs.HubDispatcher.Connect(IHub hub) at Microsoft.AspNet.SignalR.Hubs.HubPipelineModule.<>c__DisplayClass9.b__7(IHub hub) at Microsoft.AspNet.SignalR.Hubs.HubPipelineModule.<>c__DisplayClass9.b__7(IHub hub) at Microsoft.AspNet.SignalR.Hubs.HubPipeline.Connect(IHub hub) at Microsoft.AspNet.SignalR.Hubs.HubDispatcher.b__1c(IHub hub) at Microsoft.AspNet.SignalR.Hubs.HubDispatcher.<>c__DisplayClass33.b__2f(IHub instance) at System.Linq.Enumerable.WhereSelectListIterator2.MoveNext() at System.Linq.Buffer1..ctor(IEnumerable1 source) at System.Linq.Enumerable.ToArray[TSource](IEnumerable1 source) at Microsoft.AspNet.SignalR.Hubs.HubDispatcher.ExecuteHubEvent(IRequest request, String connectionId, Func2 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(IDictionary2 environment) at Microsoft.AspNet.SignalR.Owin.Middleware.HubDispatcherMiddleware.Invoke(IOwinContext context) at Microsoft.Owin.Mapping.MapMiddleware.d__0.MoveNext() InnerException: System.Runtime.Serialization.SerializationException HResult=-2146233076 Message=Unable to find assembly 'OurApp.Authorization.Models, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. Source=mscorlib StackTrace: at System.Runtime.Serialization.Formatters.Binary.BinaryAssemblyInfo.GetAssembly() at System.Runtime.Serialization.Formatters.Binary.ObjectReader.GetType(BinaryAssemblyInfo assemblyInfo, String name) at 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) at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.ReadObjectWithMapTyped(BinaryObjectWithMapTyped record) at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.Run() at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage) at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage) at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, IMethodCallMessage methodCallMessage) at System.Security.Claims.ClaimsPrincipal.DeserializeIdentities(String identities) at System.Security.Claims.ClaimsPrincipal.OnDeserializedMethod(StreamingContext context) at System.AppDomain.get_Evidence() at System.AppDomain.get_Evidence() at System.Configuration.ClientConfigPaths.GetEvidenceInfo(AppDomain appDomain, String exePath, String& typeName) at System.Configuration.ClientConfigPaths.GetTypeAndHashSuffix(AppDomain appDomain, String exePath) at System.Configuration.ClientConfigPaths..ctor(String exePath, Boolean includeUserConfig) at System.Configuration.ClientConfigPaths.GetPaths(String exePath, Boolean includeUserConfig) at System.Configuration.ClientConfigurationHost.get_ConfigPaths() at System.Configuration.ClientConfigurationHost.RequireCompleteInit(IInternalConfigRecord record) at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject) at System.Configuration.BaseConfigurationRecord.GetSection(String configKey, Boolean getLkg, Boolean checkPermission) at System.Configuration.BaseConfigurationRecord.GetSection(String configKey) at System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(String sectionName) at System.Configuration.ConfigurationManager.GetSection(String sectionName) at System.Xml.XmlConfiguration.XmlReaderSection.get_ProhibitDefaultUrlResolver() at System.Xml.XmlConfiguration.XmlReaderSection.CreateDefaultResolver() at System.Xml.Schema.Parser..ctor(SchemaType schemaType, XmlNameTable nameTable, SchemaNames schemaNames, ValidationEventHandler eventHandler) at System.Xml.Schema.XmlSchema.Read(XmlReader reader, ValidationEventHandler validationEventHandler) at System.Xml.Schema.XmlSchema.Read(Stream stream, ValidationEventHandler validationEventHandler) at System.Data.Entity.Core.SchemaObjectModel.Schema.SomSchemaSetHelper.AddXmlSchemaToSet(XmlSchemaSet schemaSet, XmlSchemaResource schemaResource, HashSet1 schemasAlreadyAdded) at System.Data.Entity.Core.SchemaObjectModel.Schema.SomSchemaSetHelper.ComputeSchemaSet(SchemaDataModelOption dataModel) at System.Data.Entity.Core.Common.Utils.Memoizer2.<>c__DisplayClass2.b__0() at System.Data.Entity.Core.Common.Utils.Memoizer2.Result.GetValue() at System.Data.Entity.Core.Common.Utils.Memoizer2.Evaluate(TArg arg) at System.Data.Entity.Core.SchemaObjectModel.Schema.SomSchemaSetHelper.GetSchemaSet(SchemaDataModelOption dataModel) at System.Data.Entity.Core.SchemaObjectModel.Schema.CreateXmlReaderSettings() at System.Data.Entity.Core.SchemaObjectModel.Schema.Parse(XmlReader sourceReader, String sourceLocation) at System.Data.Entity.Core.SchemaObjectModel.SchemaManager.ParseAndValidate(IEnumerable1 xmlReaders, IEnumerable1 sourceFilePaths, SchemaDataModelOption dataModel, AttributeValueNotification providerNotification, AttributeValueNotification providerManifestTokenNotification, ProviderManifestNeeded providerManifestNeeded, IList1& schemaCollection) at System.Data.Entity.Core.SchemaObjectModel.SchemaManager.ParseAndValidate(IEnumerable1 xmlReaders, IEnumerable1 sourceFilePaths, SchemaDataModelOption dataModel, DbProviderManifest providerManifest, IList1& schemaCollection) at System.Data.Entity.Core.SchemaObjectModel.SchemaManager.LoadProviderManifest(XmlReader xmlReader, String location, Boolean checkForSystemNamespace, Schema& schema) at System.Data.Entity.Core.Common.DbXmlEnabledProviderManifest.Load(XmlReader reader) at System.Data.Entity.Core.Common.DbXmlEnabledProviderManifest..ctor(XmlReader reader) at System.Data.Entity.SqlServer.SqlProviderManifest..ctor(String manifestToken) at System.Data.Entity.SqlServer.SqlProviderServices.b__f(String s) at System.Collections.Concurrent.ConcurrentDictionary2.GetOrAdd(TKey key, Func2 valueFactory) at System.Data.Entity.SqlServer.SqlProviderServices.GetDbProviderManifest(String versionHint) at System.Data.Entity.Core.Common.DbProviderServices.GetProviderManifest(String manifestToken) InnerException:

Emmet
  • 11
  • 3
  • Which version of EF you are using? EF has nothing to do with Windows Principal. One problem I can think of is, EF EDMX based model does not work correctly if model is loaded from referenced dll. This is actually your application which is unable load assembly into its appDomain due to restrictions. – Akash Kava Sep 03 '14 at 18:19
  • I think it's premature to say that "EntityFramework needs the Thread.CurrentPrincipal set to the current WindowsIdentity identity". Probably it needs to be able to load the assembly containing the Type that's set as `Thread.CurrentPrincipal`. Posting more details, e.g. the full exception including stack trace might help, as might the assembly binder log. – Joe Sep 03 '14 at 18:27
  • Thanks Akasha & Joe for your feedback. The version of EF is 6.1.1. Yes, I think you're right that EntityFramework needs to be able to load the assembly containing the Type that's set in the Thread.CurrentPrincipal, our CustomIdentity above. I'm just not sure where how how to do that. Than0sk again. – Emmet Sep 04 '14 at 09:16
  • After further digging I came across the following articles [here](http://stackoverflow.com/questions/19091534/claimsidentity-nullreferenceexception-after-installing-net-4-5), [here](http://stackoverflow.com/questions/15693262/serialization-exception-in-net-4-5), [here](http://stackoverflow.com/questions/313243/how-to-solve-must-be-marshalbyrefobject-in-a-good-but-multiple-inheritance-amp), and [here](http://stackoverflow.com/questions/7539557/type-cant-be-resolve-in-unittest-after-migrating-project-from-vs2005-to-vs2010). I think I'm closer but still no solution. I've update to .Net 4.5.1. – Emmet Sep 04 '14 at 15:49
  • I was going to try to use 'MarshalByRefObject' to see if that would work but I really need to my CustomIdentity to inherit from ClaimsIdentity. – Emmet Sep 04 '14 at 15:58

0 Answers0