C# ASP.NET MVC 5 是否与 WebMatrix SimpleMembershipProvider 不兼容?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19169846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:18:39  来源:igfitidea点击:

Is ASP.NET MVC 5 incompatible with the WebMatrix SimpleMembershipProvider?

c#asp.net-mvcsimplemembershipasp.net-mvc-5asp.net-identity

提问by Sixten Otto

We have an existing application that was build on ASP.NET MVC 4 & Web API. The admin parts of the site use Simple Membership. I'm interested in upgrading the application to MVC 5 / Web API 2, to take advantage of some of the new features that have been added. But it looks like they might be incompatible.

我们有一个基于 ASP.NET MVC 4 和 Web API 的现有应用程序。网站的管理部分使用简单会员。我有兴趣将应用程序升级到 MVC 5/Web API 2,以利用一些已添加的新功能。但看起来它们可能不兼容。

Specifically, after installing the RC packages from NuGet into one of the projects in my solution, and updating the web.config information, the application starts dying during startup on the line that calls WebSecurity.InitializeDatabaseConnection(), with this exception:

具体来说,在将 NuGet 中的 RC 包安装到我的解决方案中的项目之一并更新 web.config 信息后,应用程序在调用 的线路上的启动过程中开始死亡WebSecurity.InitializeDatabaseConnection(),但此例外:

[MethodAccessException: Attempt by security transparent method 'WebMatrix.WebData.PreApplicationStartCode.OnConnectionOpened(System.Object, WebMatrix.Data.ConnectionEventArgs)' to access security critical method 'System.Web.WebPages.HttpContextExtensions.RegisterForDispose(System.Web.HttpContextBase, System.IDisposable)' failed.]
   WebMatrix.WebData.PreApplicationStartCode.OnConnectionOpened(Object sender, ConnectionEventArgs e) +70
   WebMatrix.Data.Database.OnConnectionOpened() +70
   WebMatrix.Data.Database.EnsureConnectionOpen() +51
   WebMatrix.Data.Database.QueryValue(String commandText, Object[] args) +63
   WebMatrix.WebData.DatabaseWrapper.QueryValue(String commandText, Object[] parameters) +13
   WebMatrix.WebData.SimpleMembershipProvider.GetUserId(IDatabase db, String userTableName, String userNameColumn, String userIdColumn, String userName) +206
   WebMatrix.WebData.SimpleMembershipProvider.ValidateUserTable() +87

Other projects in the same solution using Simple Membership that I have notupgraded continue to work just fine.

使用 Simple Membership 的同一个解决方案中的其他项目我没有升级,继续工作得很好。

Googling around for more information turns up lots of hits for that exception, of course, but nothing particular to WebMatrix.

当然,谷歌搜索更多信息会发现该异常的大量点击,但没有特别针对 WebMatrix。

FWIW: I know that Microsoft has introduced (yet another) membership and identity solution, but unless there's a way to use that with the existing Simple Membership tables, or a seamless migration path for all of our existing user data, that's not really an option for us.

FWIW:我知道微软已经引入了(又一个)会员和身份解决方案,但除非有一种方法可以将它与现有的简单会员表一起使用,或者我们所有现有用户数据的无缝迁移路径,否则这不是一个真正的选择为我们。

UPDATE (11 Oct)

更新(10 月 11 日)

I just tried this again with a fresh checkout of the current trunk of our app. I'm using Visual Studio 2012, but otherwise followed the instructions from MS for upgrading an existing project. After updating to MVC 5 / Web API 2 / EF 6, the app started up an ran just fine.

我只是通过对我们应用程序当前主干的全新结帐再次尝试了这一点。我正在使用 Visual Studio 2012,但在其他方面按照 MS 的说明升级现有项目。更新到 MVC 5 / Web API 2 / EF 6 后,应用程序启动并运行得很好。

There were no explicit trust requirements in the web.configto remove. I added the code from this questionto Global.asax.cs, and it reports that the app is running with full trust (in IIS Express, just F5-ed from VS).

web.config删除中没有明确的信任要求。我将这个问题中的代码添加到Global.asax.cs,它报告应用程序在完全信任的情况下运行(在 IIS Express 中,只是从 VS 中按 F5 键)。

Re-adding the same call to InitializeDatabaseConnection(), it starts dying with the exact same exception.

将相同的调用重新添加到InitializeDatabaseConnection(),它会以完全相同的异常开始死亡。

SOLUTION (28 Oct)

解决方案(10 月 28 日)

Trying the solution in @Kevin's update from Friday, I found that it works. It was really strange to me that adding this apparently unrelated package would solve these security issues, and even morestrange after I removed the package from my solution, and it kept working.

从周五开始尝试@Kevin 更新中的解决方案,我发现它有效。我真的很奇怪,添加这个明显不相关的包会解决这些安全问题,奇怪的是我从我的解决方案中删除了这个包,它继续工作

Taking a closer look at what was happening, I realized that the reason why this fixes the behavior is quite simple: the Microsoft.AspNet.WebHelperspackage has two dependencies that were being added to my solution: Microsoft.AspNet.WebPages.Dataand Microsoft.AspNet.WebPages.WebData. Microsoft has moved the WebMatrix classes into new packages.

仔细看看发生了什么,我意识到修复行为的原因很简单:该Microsoft.AspNet.WebHelpers包有两个依赖项被添加到我的解决方案中:Microsoft.AspNet.WebPages.DataMicrosoft.AspNet.WebPages.WebData. Microsoft 已将 WebMatrix 类移动到新包中。

So added the helpers package fixed the problem, not because of anything it was doing, but because it was causing updated versions of the broken assemblies to be addedto my solution. The solution to the initial incompatibility, then, is to install these new packages when updating everything else from NuGet:

所以添加了 helpers 包解决了这个问题,不是因为它在做什么,而是因为它导致损坏的程序集的更新版本被添加到我的解决方案中。因此,最初不兼容的解决方案是在从 NuGet 更新其他所有内容时安装这些新包:

Install-Package Microsoft.AspNet.WebPages.WebData

UPDATE (13 May 2015)

更新(2015 年 5 月 13 日)

It has been suggested to methat you may also need to manually install the second new package:

有人向我建议,你可能还需要手动安装第二个新包:

Install-Package Microsoft.AspNet.WebPages.Data

This should notbe necessary, because this package is an explicit dependencyof the first, and NuGet should be smart enough to install both. But if you get an error when building, or don't see NuGet add the dependency, it might help you.

应该不是必需的,因为这个包是第一个的显式依赖项,并且 NuGet 应该足够聪明来安装这两个包。但是,如果您在构建时遇到错误,或者没有看到 NuGet 添加了依赖项,它可能会对您有所帮助。

采纳答案by Kevin Junghans

WebMatrix is compatible with MVC 5.

WebMatrix 与 MVC 5 兼容。

What I did was to take an empty MVC 5 project and incorporate WebMatrix SimpleMembershipProvider into it using SimpleSecurity, an open source project that decouples SimpleMembership from your MVC application. So far I am able to create the database, seed it, and log in and out. I plan on adding other features to this reference application, such as email confirmation and various tests. When I am done I will post the source code in the SimpleSecurity Project

我所做的是采用一个空的 MVC 5 项目,并使用SimpleSecurity将 WebMatrix SimpleMembershipProvider 合并到其中,这是一个将 SimpleMembership 与您的 MVC 应用程序分离的开源项目。到目前为止,我已经能够创建数据库、播种以及登录和注销。我计划向此参考应用程序添加其他功能,例如电子邮件确认和各种测试。完成后,我将在SimpleSecurity 项目中发布源代码

If I had to guess, your problem may be with the upgrade process. What process did you take to upgrade your MVC 4 project to MVC 5? Did you follow this process? What version of the WebMatrix assemblies are you using? What version of Visual Studio are you using? I am using version 2.0.0.0 of WebMatrix and Visual Studio 2013 RC.

如果我不得不猜测,您的问题可能与升级过程有关。您采用什么过程将 MVC 4 项目升级到 MVC 5? 你遵循了这个过程吗?您使用的是什么版本的 WebMatrix 程序集?您使用的是哪个版本的 Visual Studio?我使用的是 2.0.0.0 版的 WebMatrix 和 Visual Studio 2013 RC。



Update (10/25/2013)

更新 (10/25/2013)

I continued my experiment with adding SimpleMembership to an MVC 5 project and somewhere along the line it broke and I got the same results as @Sixten Otto. I did not test incrementally as I added things but I am suspicious it may have happened when I installed the Web API assemblies. They are not installed by default when creating a new MVC 5 project.

我继续我的实验,将 SimpleMembership 添加到 MVC 5 项目中,但它在某个地方失败了,我得到了与 @Sixten Otto 相同的结果。我在添加内容时没有进行增量测试,但我怀疑这可能是在我安装 Web API 程序集时发生的。创建新的 MVC 5 项目时,默认情况下不会安装它们。

I did some more research on the error and came across this QA titled "Attempt by security transparent method 'WebMatrix.WebData.PreApplicationStartCode.Start()'". This is an old QA and originally someone was getting this same error when upgrading an MVC 3 app to MVC 4. But recently people have been adding answers in regards to upgrading to MVC 5 and one of the answers worked for me. The solution for me was install the NuGet package Microsoft.AspNet.WebHelpers.After installing this package everything worked fine.

我对该错误进行了更多研究,并遇到了标题为“尝试通过安全透明方法'WebMatrix.WebData.PreApplicationStartCode.Start()'”的QA 。这是一个旧的 QA,最初有人在将 MVC 3 应用程序升级到 MVC 4 时遇到了同样的错误。但最近人们一直在添加有关升级到 MVC 5 的答案,其中一个答案对我有用。 我的解决方案是安装 NuGet 包Microsoft.AspNet.WebHelpers安装此软件包后一切正常。

A note about my research into migrating to the new ASP.NET Identity is that they do not use the same password hash, which precludes moving old members into a database used by ASP.NET Identity. ASP.NET Identity seems to be in real flux right now so maybe they will come up with a solution for this.

关于我迁移到新 ASP.NET Identity 的研究的一个说明是,它们不使用相同的密码哈希,这妨碍了将旧成员移动到 ASP.NET Identity 使用的数据库中。ASP.NET Identity 现在似乎在不断变化,所以也许他们会为此想出一个解决方案。



Update (2/16/14)

更新 (2/16/14)

I erroneously reported that the hash algorithm for passwords was different in SimpleMembership and ASP.NET Identity. I assumed this based on a visual inspection of the hashed passwords, assuming that it was just the hashed password that was in the fields. After further research I found that SimpleMembership uses the System.Web.Helpers.Crypto class for hashing the password and what is stored in the password field is actually a 256 bit subkey and the salt. With that information I ran some tests to validate that ASP.NET Identity can verify passwords that are generated by SimpleMembership, and it passed. I was trying to find out what hash algorithm SimpleMembership used so I could plug in a password hasher in ASP.NET Identity that would allow me to migrate data from a SimpleMembership webiste to one that used ASP.NET Identity. Turns out it is not necessary. I talk about the password hash and how to migrate the data from SimpleMembership to ASP.NET Identity in more detail in this article.

我错误地报告了 SimpleMembership 和 ASP.NET Identity 中密码的哈希算法不同。我假设这是基于对散列密码的目视检查,假设它只是字段中的散列密码。经过进一步研究,我发现 SimpleMembership 使用 System.Web.Helpers.Crypto 类对密码进行哈希处理,而密码字段中存储的实际上是一个 256 位的子密钥和盐。有了这些信息,我运行了一些测试来验证 ASP.NET Identity 可以验证 SimpleMembership 生成的密码,并且它通过了。我试图找出 SimpleMembership 使用的哈希算法,以便我可以在 ASP.NET Identity 中插入密码哈希器,这将允许我将数据从 SimpleMembership 网站迁移到使用 ASP.NET Identity 的网站。我在本文中更详细地讨论了密码哈希以及如何将数据从 SimpleMembership 迁移到 ASP.NET Identity

回答by pranav rastogi

We are currently working on a migration doc for migrating from Simple Membership to ASP.NET Identity. Please stay tuned for a couple of weeks until we push this migration doc. For now you have to map your Simple Membership schema to Identity and change your application code to use OWIN for SignIN/ SIgnOut

我们目前正在编写迁移文档,用于从 Simple Membership 迁移到 ASP.NET Identity。请继续关注几周,直到我们推送此迁移文档。现在,您必须将 Simple Membership 架构映射到 Identity 并更改您的应用程序代码以使用 OWIN 进行登录/退出

回答by Anushka

If you are getting the error

如果您收到错误

Attempt by security transparent method ‘WebMatrix.WebData.PreApplicationStartCode.Start()' to access security critical method ‘System.Web.WebPages.Razor.WebPageRazorHost.AddGlobalImport(System.String)' failed.

尝试通过安全透明方法“WebMatrix.WebData.PreApplicationStartCode.Start()”访问安全关键方法“System.Web.WebPages.Razor.WebPageRazorHost.AddGlobalImport(System.String)”失败。

In order to fix this install this package using NuGet package manager.

为了解决此问题,请使用 NuGet 包管理器安装此包。

Install-Package Microsoft.AspNet.WebHelpers

After that , probably you will get another error

在那之后,您可能会遇到另一个错误

Cannot load WebMatrix.Data version 3.0.0.0 assembly

无法加载 WebMatrix.Data 3.0.0.0 版程序集

to fix this install this package using NuGet package manager.

要修复此问题,请使用 NuGet 包管理器安装此包。

Install-Package Microsoft.AspNet.WebPages.Data

回答by Ifeanyi Chukwu

The above answers worked not until recent webpages 3.2.3. A new issue showed up for me. The current fix for me was by upgrading to .Net 4.5.3. I figured this out of frustration. This issue doesnt just affect MVC 5 but core Webmatrix projects as well after upgrading to webpages 3.2.3. I think it is a framework issue that will be fixed with the new Microsoft Identity. The current fix for me is below: Note: Please use the property pages wizard in visual studio to change your target framework to .Net Framework 4.5.3. It will update your web.config

上述答案直到最近的网页 3.2.3 才有效。一个新问题出现在我面前。我目前的修复方法是升级到 .Net 4.5.3。我是出于沮丧想到了这一点。升级到网页 3.2.3 后,此问题不仅会影响 MVC 5,还会影响核心 Webmatrix 项目。我认为这是一个框架问题,将通过新的 Microsoft Identity 来解决。我的当前修复如下: 注意:请使用 Visual Studio 中的属性页向导将目标框架更改为 .Net Framework 4.5.3。它会更新你的 web.config

<compilation debug="true" targetFramework="4.5.3">
  <assemblies>
    <add assembly="System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
  </assemblies>
</compilation>

Step 1: Install-Package Microsoft.AspNet.WebHelpers

步骤 1:安装包 Microsoft.AspNet.WebHelpers

Step 2: Install-Package Microsoft.AspNet.WebPages.Data

步骤 2:安装包 Microsoft.AspNet.WebPages.Data

Step 3: [Optional] Install-Package Owin

第 3 步:[可选] 安装包 Owin

Step 4: Change targetFramework to .Net 4.5.3 via Property pages dialog box

步骤 4:通过属性页对话框将 targetFramework 更改为 .Net 4.5.3

enter image description here

在此处输入图片说明

[Optionally] Your Web.Config should look like the below

[可选] 您的 Web.Config 应如下所示

    <?xml version="1.0"?>

<configuration>
  <appSettings/>
  <connectionStrings>
    <add connectionString="Server=XTREMEGOSPEL;Database=portfolioDB;Trusted_Connection=True" name="portDB" providerName="System.Data.SqlClient"/>
  </connectionStrings>
  <!--
    For a description of web.config changes see http://go.microsoft.com/fwlink/?LinkId=235367.

    The following attributes can be set on the <httpRuntime> tag.
      <system.Web>
        <httpRuntime targetFramework="4.5" />
      </system.Web>
  -->
  <system.web>
    <compilation debug="true" targetFramework="4.5.3">
      <assemblies>
        <add assembly="System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
      </assemblies>
    </compilation>
    <httpRuntime targetFramework="4.5" maxRequestLength="2097151"/>
    <authentication mode="Forms">
      <forms timeout="1440"/>
    </authentication>
    <sessionState timeout="1440"/>
    <pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/>
  </system.web>
  <system.webServer>
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="4294967295"/>
      </requestFiltering>
    </security>
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

回答by Fatih ?elik

I got same problem, not at my local computer but live site was having that.

我遇到了同样的问题,不是在我的本地计算机上,而是在实时站点上。

I removed below lines from web config and it is working now.

我从网络配置中删除了以下几行,现在可以正常工作了。

<dependentAssembly>
   <assemblyIdentity name="System.Web.WebPages.Razor" publicKeyToken="31bf3856ad364e35" />
   <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>