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;
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);
- 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.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() at System.Data.Entity.Internal.Linq.InternalSet1.get_InternalContext() at System.Data.Entity.Infrastructure.DbQuery
1.System.Linq.IQueryable.get_Provider() at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable1 source, Expression
1 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.Buffer
1..ctor(IEnumerable1 source) at System.Linq.Enumerable.ToArray[TSource](IEnumerable
1 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(IDictionary
2 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.Memoizer
2.<>c__DisplayClass2.b__0() at System.Data.Entity.Core.Common.Utils.Memoizer2.Result.GetValue() at System.Data.Entity.Core.Common.Utils.Memoizer
2.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, IEnumerable
1 sourceFilePaths, SchemaDataModelOption dataModel, AttributeValueNotification providerNotification, AttributeValueNotification providerManifestTokenNotification, ProviderManifestNeeded providerManifestNeeded, IList1& schemaCollection) at System.Data.Entity.Core.SchemaObjectModel.SchemaManager.ParseAndValidate(IEnumerable
1 xmlReaders, IEnumerable1 sourceFilePaths, SchemaDataModelOption dataModel, DbProviderManifest providerManifest, IList
1& 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, Func
2 valueFactory) at System.Data.Entity.SqlServer.SqlProviderServices.GetDbProviderManifest(String versionHint) at System.Data.Entity.Core.Common.DbProviderServices.GetProviderManifest(String manifestToken) InnerException: