MVC5 WebSecurity,临时使用辅助数据库

MVC5 WebSecurity, use secondary database temporarily

我有一个包含两个 MVC 项目的解决方案。第一个是管理系统,第二个是承包商门户。我想通过主应用程序管理门户网站的承包商登录。

两个项目的成员数据库在架构上是相同的(默认)。我的想法是劫持主系统的注册操作,换出连接字符串,在自己的数据库中注册承包商用户,然后将连接字符串换回主系统。

我猜这不会起作用,即基于 WebSecurity.InitializeDatabaseConnection() 只能被调用一次的事实。

因此我的问题是:如果我想调用 WebSecurity.CreateUserAndAccount() 方法但将其指向与最初初始化时不同的数据库,我该怎么做?

我查看了内置的 InitializeSimpleMembership 过滤器以寻找线索,但没有看到我正在寻找的路径。

这种方法可行吗?

或者,如果我可以按照默认情况下相同的方式对密码进行哈希处理,那么我似乎可以手动将所需数据输入到数据库中。

看来我可以手动解决这个问题。根据 WebSecurity.CreateUserAndAccount():

的 msdn 文档

This method creates a new entry in the user profile table and then a corresponding entry in the membership table. The ID of the membership entry is based on the ID of the user profile entry. (The IDs of the entries in the two tables match.)

(https://msdn.microsoft.com/en-us/library/webmatrix.webdata.websecurity.createuserandaccount(v=vs.111).aspx)

检查数据库模式表明我只需要两个表,字段是直接的。

要按照与 SimpleMembershipProvider 相同的方式散列密码,我可以使用 System.Web.Helpers.Crypo.HashPassword() 方法。

这大大简化了基本帐户的创建,因为我不必搭载 AccountController,只需将它们直接写入数据库并称其为好(无论如何,出于我的目的,不需要验证令牌并单独拥有处理电子邮件通知)。在门户端,内置的 SimpleMembershipProvider 应该可以照常处理所有事情。