ASP.NET MVC 5 是否与 WebMatrix SimpleMembershipProvider 不兼容? [英] Is ASP.NET MVC 5 incompatible with the WebMatrix SimpleMembershipProvider?

查看:18
本文介绍了ASP.NET MVC 5 是否与 WebMatrix SimpleMembershipProvider 不兼容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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:

[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

同一解决方案中的其他项目使用我升级的 Simple Membership 继续正常工作.

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

当然,在谷歌上搜索更多信息会发现该异常的命中率很高,但对 WebMatrix 而言并没有什么特别之处.

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

FWIW:我知道微软已经引入了 (又一个)成员资格和身份解决方案,但除非有一种方法可以将其与现有的简单成员资格表或无缝迁移路径一起使用对于我们所有现有的用户数据,这对我们来说并不是一个真正的选择.

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.

我刚刚重新检查了我们应用程序的当前主干,再次尝试了这个.我正在使用 Visual Studio 2012,但在其他方面按照 MS 的说明升级现有项目.更新到 MVC 5/Web API 2/EF 6 后,该应用程序启动并运行良好.

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.

web.config 中没有要删除的明确信任要求.我将 this question 中的代码添加到 Global.asax.cs,并报告应用程序在完全信任的情况下运行(在 IIS Express 中,只需从 VS 中按 F5 键即可).

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

重新添加对 InitializeDatabaseConnection() 的相同调用,它开始以完全相同的异常死亡.

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

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

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 more strange after I removed the package from my solution, and it kept working.

仔细观察正在发生的事情,我意识到修复行为的原因很简单:Microsoft.AspNet.WebHelpers 包有两个依赖项,它们被添加到我的解决方案中:Microsoft.AspNet.WebPages.DataMicrosoft.AspNet.WebPages.WebData.Microsoft 已将 WebMatrix 类移到新包中.

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

所以添加了帮助程序包解决了这个问题,不是因为它正在做的任何事情,而是因为它导致将损坏的程序集的更新版本添加到我的解决方案中.因此,解决最初不兼容的方法是在从 NuGet 更新其他所有内容时安装这些新包:

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 added to my solution. The solution to the initial incompatibility, then, is to install these new packages when updating everything else from NuGet:

Install-Package Microsoft.AspNet.WebPages.WebData

更新(2015 年 5 月 13 日)

向我建议您可能还需要手动安装第二个新软件包:>

UPDATE (13 May 2015)

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

Install-Package Microsoft.AspNet.WebPages.Data

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

This should not be necessary, because this package is an explicit dependency of 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.

推荐答案

WebMatrix 与 MVC 5 兼容.

WebMatrix is compatible with MVC 5.

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

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 4 项目升级到 MVC 5?您是否遵循此流程?您使用的是什么版本的 WebMatrix 程序集?您使用的是哪个版本的 Visual Studio?我使用的是 WebMatrix 2.0.0.0 版和 Visual Studio 2013 RC.

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.

更新(10/25/2013)

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

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.

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

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.

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

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.

更新 (2/16/14)

我错误地报告了 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.

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.

这篇关于ASP.NET MVC 5 是否与 WebMatrix SimpleMembershipProvider 不兼容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆