0

我正在尝试将 asp 身份添加到一个非常基本的 MVC 站点,该站点当前包含来自 SQL 数据库的单个表。

SQL 被添加为 ADO.NET 实体数据模型,使用数据库中的 EF 设计器,我可以从我的 MVC 站点(在本地开发和发布后)毫无问题地进行读写。

然后我添加了 asp.net 身份,这添加了一个有效的本地数据库文件。

然后,我尝试将身份表迁移到我的主 SQL 服务器,这样我只需要维护一个数据库(网站的托管和 SQL 数据库都使用同一个提供程序)。

我已设法在主 SQL DB 中创建表,并在我的 web.config 中添加了第二个连接字符串,以允许 asp 身份和我现有的 edmx 设置工作(如果我尝试通过 EF 连接字符串运行 asp 身份我得到错误“实体类型 ApplicationUser 不是当前上下文模型的一部分。”)。然后我删除了本地数据库文件以确保。

在我的客户端(使用两个连接字符串)上进行测试时,我可以创建帐户、登录和退出并按预期使用 asp.identity,还可以看到主 SQL DB 中的身份表正在填充。我还可以使用我的通用 EF 连接来读取和写入数据库的核心 MVC 站点。

但是,当我发布到主站点时,无论我调用身份还是 EF DB 函数,我都会收到错误消息

错误。处理您的请求时发生错误。

它显示在渲染页面上/内联(我尝试在 web.config 中打开自定义错误,但这似乎没有给我更多详细信息。)

我的两个连接字符串如下所示(我假设这就是问题所在?)我还需要其他什么web.config吗?还是我错过了一些基本的东西?

<connectionStrings>
   <add name="BDIdentityString" connectionString="Data Source=IPADDRESS; Initial Catalog=DBNAME; User ID=USERNAME; Password=PASSWORD; Connect Timeout=60;" providerName="System.Data.SqlClient" />
   <add name="BDString" connectionString="metadata=res://*/Models.DBENTITYNAME.csdl|res://*/Models.DBENTITYNAME.ssdl|res://*/Models.DBENTITYNAME.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=IPADDRESS;initial catalog=DBNAME;user id=USERNAME;password=PASSWORD;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
 </connectionStrings>

更新:

所以我终于得到了自定义错误(认为我在某处有错字),错误发布在下面。作为测试,我使用我的项目的备份副本在不引入身份的情况下再次测试 EF DB 查询 - 发布并实时测试。再次,这一切都按预期工作。然后我恢复了我当前的构建并添加了身份并回到相同的问题。(EF 的连接字符串在项目的两个版本中保持不变)

为清楚起见,我使用的是 fasthosts.co.uk,在我的测试中,我通过从我的客户端读取和写入由 fasthosts 托管的实时 SQL 数据库来做到这一点。与发布到 fasthosts 时没有身份的备份版本一样,仍然能够连接和读取/写入 SQL DB。

我正在使用他们的 Momentum 包,因此包含了从我拥有的本地 IIS 服务器生成的所需的 machineKey。此密钥在没有身份的版本 1 和有身份的版本 2 中都存在。

我还尝试将以下内容添加到 AssemblyInfo.cs

[assembly: AllowPartiallyTrustedCallers]
[assembly: SecurityRules(SecurityRuleSet.Level1)]

(fasthosts 推荐的 AllowPartiallyTrustedCallers 用于安全异常描述类型错误)

关闭 customerErrors 后,我会得到两个不同的响应,这取决于我是通过身份调用数据库还是从我的 MVC 代码中的 EF 调用调用数据库。错误是这样的:

EF错误:

erver Error in '/' Application.

Operation could destabilize the runtime. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Security.VerificationException: Operation could destabilize the runtime.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. 

Stack Trace: 


[VerificationException: Operation could destabilize the runtime.]
   System.Data.Entity.Core.Metadata.Edm.FacetDescription.Validate(String declaringTypeName) +70
   System.Data.Entity.Core.Metadata.Edm.FacetDescription..ctor(String facetName, EdmType facetType, Nullable`1 minValue, Nullable`1 maxValue, Object defaultValue, Boolean isConstant, String declaringTypeName) +103
   System.Data.Entity.Core.SchemaObjectModel.FacetDescriptionElement.CreateAndValidateFacetDescription(String declaringTypeName) +115
   System.Data.Entity.Core.SchemaObjectModel.TypeElement.ResolveTopLevelNames() +119
   System.Data.Entity.Core.SchemaObjectModel.Schema.ResolveTopLevelNames() +206
   System.Data.Entity.Core.SchemaObjectModel.Schema.Resolve() +16
   System.Data.Entity.Core.SchemaObjectModel.SchemaManager.ParseAndValidate(IEnumerable`1 xmlReaders, IEnumerable`1 sourceFilePaths, SchemaDataModelOption dataModel, AttributeValueNotification providerNotification, AttributeValueNotification providerManifestTokenNotification, ProviderManifestNeeded providerManifestNeeded, IList`1& schemaCollection) +553
   System.Data.Entity.Core.SchemaObjectModel.SchemaManager.ParseAndValidate(IEnumerable`1 xmlReaders, IEnumerable`1 sourceFilePaths, SchemaDataModelOption dataModel, DbProviderManifest providerManifest, IList`1& schemaCollection) +156
   System.Data.Entity.Core.SchemaObjectModel.SchemaManager.LoadProviderManifest(XmlReader xmlReader, String location, Boolean checkForSystemNamespace, Schema& schema) +139
   System.Data.Entity.Core.Common.DbXmlEnabledProviderManifest.Load(XmlReader reader) +75
   System.Data.Entity.Core.Common.DbXmlEnabledProviderManifest..ctor(XmlReader reader) +116
   System.Data.Entity.SqlServer.SqlProviderManifest..ctor(String manifestToken) +37
   System.Data.Entity.SqlServer.<>c.<GetDbProviderManifest>b__29_0(String s) +25
   System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory) +73
   System.Data.Entity.SqlServer.SqlProviderServices.GetDbProviderManifest(String versionHint) +124
   System.Data.Entity.Core.Common.DbProviderServices.GetProviderManifest(String manifestToken) +55

[ProviderIncompatibleException: The provider did not return a ProviderManifest instance.]
   System.Data.Entity.Core.Common.DbProviderServices.GetProviderManifest(String manifestToken) +186
   System.Data.Entity.Core.Metadata.Edm.Loader.InitializeProviderManifest(Action`3 addError) +312
   System.Data.Entity.Core.Metadata.Edm.Loader.OnProviderManifestTokenNotification(String token, Action`3 addError) +33
   System.Data.Entity.Core.SchemaObjectModel.Schema.HandleProviderManifestTokenAttribute(XmlReader reader) +97
   System.Data.Entity.Core.SchemaObjectModel.Schema.HandleAttribute(XmlReader reader) +184
   System.Data.Entity.Core.SchemaObjectModel.SchemaElement.ParseAttribute(XmlReader reader) +185
   System.Data.Entity.Core.SchemaObjectModel.SchemaElement.Parse(XmlReader reader) +76
   System.Data.Entity.Core.SchemaObjectModel.Schema.HandleTopLevelSchemaElement(XmlReader reader) +37
   System.Data.Entity.Core.SchemaObjectModel.Schema.InternalParse(XmlReader sourceReader, String sourceLocation) +739
   System.Data.Entity.Core.SchemaObjectModel.Schema.Parse(XmlReader sourceReader, String sourceLocation) +58
   System.Data.Entity.Core.SchemaObjectModel.SchemaManager.ParseAndValidate(IEnumerable`1 xmlReaders, IEnumerable`1 sourceFilePaths, SchemaDataModelOption dataModel, AttributeValueNotification providerNotification, AttributeValueNotification providerManifestTokenNotification, ProviderManifestNeeded providerManifestNeeded, IList`1& schemaCollection) +340
   System.Data.Entity.Core.Metadata.Edm.Loader.LoadItems(IEnumerable`1 xmlReaders, IEnumerable`1 sourceFilePaths) +158
   System.Data.Entity.Core.Metadata.Edm.Loader..ctor(IEnumerable`1 xmlReaders, IEnumerable`1 sourceFilePaths, Boolean throwOnError, IDbDependencyResolver resolver) +127
   System.Data.Entity.Core.Metadata.Edm.StoreItemCollection.Init(IEnumerable`1 xmlReaders, IEnumerable`1 filePaths, Boolean throwOnError, IDbDependencyResolver resolver, DbProviderManifest& providerManifest, DbProviderFactory& providerFactory, String& providerInvariantName, String& providerManifestToken, Memoizer`2& cachedCTypeFunction) +131
   System.Data.Entity.Core.Metadata.Edm.StoreItemCollection..ctor(IEnumerable`1 xmlReaders, IEnumerable`1 filePaths) +223
   System.Data.Entity.Core.Metadata.Edm.MetadataCache.LoadStoreCollection(EdmItemCollection edmItemCollection, MetadataArtifactLoader loader) +88
   System.Data.Entity.Core.Metadata.Edm.<>c__DisplayClass9_1.<GetMetadataWorkspace>b__1() +14
   System.Lazy`1.CreateValue() +429
   System.Lazy`1.LazyInitValue() +158
   System.Lazy`1.get_Value() +79
   System.Data.Entity.Core.Metadata.Edm.<>c__DisplayClass9_1.<GetMetadataWorkspace>b__4() +9
   System.Data.Entity.Core.Metadata.Edm.MetadataWorkspace.LoadAndCheckItemCollection(Func`1 itemCollectionLoader) +16
   System.Data.Entity.Core.Metadata.Edm.<>c__DisplayClass12_0.<.ctor>b__3() +21
   System.Lazy`1.CreateValue() +429
   System.Lazy`1.LazyInitValue() +158
   System.Lazy`1.get_Value() +79
   System.Data.Entity.Core.Metadata.Edm.MetadataWorkspace.GetItemCollection(DataSpace dataSpace, Boolean required) +95
   System.Data.Entity.Core.Metadata.Edm.MetadataWorkspace.GetItemCollection(DataSpace dataSpace) +12
   System.Data.Entity.Core.Objects.ObjectContext.InitializeMappingViewCacheFactory(DbContext owner) +46
   System.Data.Entity.Core.Objects.ObjectContext..ctor(EntityConnection connection, Boolean isConnectionConstructor, ObjectQueryExecutionPlanFactory objectQueryExecutionPlanFactory, Translator translator, ColumnMapFactory columnMapFactory) +575
   System.Data.Entity.Internal.InternalConnection.CreateObjectContextFromConnectionModel() +65
   System.Data.Entity.Internal.LazyInternalConnection.CreateObjectContextFromConnectionModel() +21
   System.Data.Entity.Internal.LazyInternalContext.InitializeContext() +506
   System.Data.Entity.Internal.InternalContext.Initialize() +20
   System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +16
   System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() +53
   System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext() +15
   System.Data.Entity.Internal.Linq.InternalSet`1.ActOnSet(Action action, EntityState newState, Object entity, String methodName) +62
   System.Data.Entity.Internal.Linq.InternalSet`1.Add(Object entity) +108
   System.Data.Entity.DbSet`1.Add(TEntity entity) +72
   HotBrewCrew.Controllers.HomeController.Register(interested_parties fromForm) in C:\Users\ewilson\Documents\Development\HotBrewCrew\HotBrewCrew\Controllers\HomeController.cs:26
   lambda_method(Closure , ControllerBase , Object[] ) +100
   System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +14
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +166
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +27
   System.Web.Mvc.Async.<>c.<BeginInvokeSynchronousActionMethod>b__9_0(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +22
   System.Web.Mvc.Async.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult) +29
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +32
   System.Web.Mvc.Async.<>c__DisplayClass11_0.<InvokeActionMethodFilterAsynchronouslyRecursive>b__0() +58
   System.Web.Mvc.Async.<>c__DisplayClass11_2.<InvokeActionMethodFilterAsynchronouslyRecursive>b__2() +228
   System.Web.Mvc.Async.<>c__DisplayClass7_0.<BeginInvokeActionMethodWithFilters>b__1(IAsyncResult asyncResult) +10
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +34
   System.Web.Mvc.Async.<>c__DisplayClass3_6.<BeginInvokeAction>b__4() +35
   System.Web.Mvc.Async.<>c__DisplayClass3_1.<BeginInvokeAction>b__1(IAsyncResult asyncResult) +100
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27
   System.Web.Mvc.<>c.<BeginExecuteCore>b__152_1(IAsyncResult asyncResult, ExecuteCoreState innerState) +11
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +45
   System.Web.Mvc.<>c.<BeginExecute>b__151_2(IAsyncResult asyncResult, Controller controller) +13
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +22
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +26
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
   System.Web.Mvc.<>c.<BeginProcessRequest>b__20_1(IAsyncResult asyncResult, ProcessRequestState innerState) +28
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +28
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9839161
   System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +50
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +163

和身份错误

Server Error in '/' Application.

Inheritance security rules violated while overriding member: 'Microsoft.AspNet.Identity.TaskExtensions+CultureAwaiter`1<T>.UnsafeOnCompleted(System.Action)'. Security accessibility of the overriding method must match the security accessibility of the method being overriden. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.TypeLoadException: Inheritance security rules violated while overriding member: 'Microsoft.AspNet.Identity.TaskExtensions+CultureAwaiter`1<T>.UnsafeOnCompleted(System.Action)'. Security accessibility of the overriding method must match the security accessibility of the method being overriden.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. 

Stack Trace: 


[TypeLoadException: Inheritance security rules violated while overriding member: 'Microsoft.AspNet.Identity.TaskExtensions+CultureAwaiter`1<T>.UnsafeOnCompleted(System.Action)'. Security accessibility of the overriding method must match the security accessibility of the method being overriden.]
   Microsoft.AspNet.Identity.Owin.SignInManager`2.PasswordSignInAsync(String userName, String password, Boolean isPersistent, Boolean shouldLockout) +0
   HotBrewCrew.Controllers.<Login>d__11.MoveNext() in C:\Users\ewilson\Documents\Development\HotBrewCrew\HotBrewCrew\Controllers\AccountController.cs:74
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +102
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +64
   System.Web.Mvc.Async.TaskAsyncActionDescriptor.EndExecute(IAsyncResult asyncResult) +97
   System.Web.Mvc.Async.<>c__DisplayClass8_0.<BeginInvokeAsynchronousActionMethod>b__1(IAsyncResult asyncResult) +17
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +32
   System.Web.Mvc.Async.<>c__DisplayClass11_0.<InvokeActionMethodFilterAsynchronouslyRecursive>b__0() +58
   System.Web.Mvc.Async.<>c__DisplayClass11_2.<InvokeActionMethodFilterAsynchronouslyRecursive>b__2() +228
   System.Web.Mvc.Async.<>c__DisplayClass7_0.<BeginInvokeActionMethodWithFilters>b__1(IAsyncResult asyncResult) +10
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +34
   System.Web.Mvc.Async.<>c__DisplayClass3_6.<BeginInvokeAction>b__4() +35
   System.Web.Mvc.Async.<>c__DisplayClass3_1.<BeginInvokeAction>b__1(IAsyncResult asyncResult) +100
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27
   System.Web.Mvc.<>c.<BeginExecuteCore>b__152_1(IAsyncResult asyncResult, ExecuteCoreState innerState) +11
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +45
   System.Web.Mvc.<>c.<BeginExecute>b__151_2(IAsyncResult asyncResult, Controller controller) +13
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +22
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +26
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
   System.Web.Mvc.<>c.<BeginProcessRequest>b__20_1(IAsyncResult asyncResult, ProcessRequestState innerState) +28
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +28
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9839161
   System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +50
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +163


Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.8.3928.0 

更新 2

这可能是 fasthosts 的信任问题。我刚刚建立了一个启用身份的开箱即用 MVC 站点。将连接字符串更改为指向 SQL DB - 再次在开发环境中工作(从主项目开发测试中注册详细信息),但在没有其他更改的情况下实时推送时失败。

我会努力询问托管服务提供商,但我不是 100% 相信这是我唯一/或实际的问题,因为 EF 在没有身份的情况下工作,只是没有在项目中安装身份。

4

1 回答 1

0

正如预期的那样,限制是https://www.fasthosts.co.uk/ - 使用提供程序https://hostinguk.net/移动我的站点而不更改代码(除了 SQL 连接字符串以反映新的 SQL 服务器)该网站工作没有问题。

于 2019-10-21T12:07:21.673 回答