简体   繁体   English

架构和ASP.Net身份

[英]Architecture and ASP.Net Identity

Im currently following the following GitHub for guidance on storing user data from ASP.Net Identity into Mongo: https://github.com/g0t4/aspnet-identity-mongo . 我目前正在遵循以下GitHub,以获取有关将用户数据从ASP.Net Identity存储到Mongo的指导: https : //github.com/g0t4/aspnet-identity-mongo With a basic proof of concept, I have been able to successfully store user data. 有了基本的概念证明,我就能够成功存储用户数据。 The current issue that I am facing is my application requires two types of users (both will be stored to the same Mongo collection). 我当前面临的问题是我的应用程序需要两种类型的用户(这两种类型的用户都将存储到同一Mongo集合中)。

For simplicity, we can call the users A and B. Both A and B are setup as models and have their own properties. 为简单起见,我们可以称呼用户A和B。A和B都被设置为模型并具有自己的属性。 My initial idea was to setup both A and B as derived classes of ApplicationUser (see the IdentityModels.cs file in the referenced repo). 我最初的想法是将A和B都设置为ApplicationUser的派生类(请参见参考存储库中的IdentityModels.cs文件)。

Registration into my application worked perfectly with this architecture and JSON documents were posted/stored into my MongoDB. 在该应用程序中的注册与该体系结构完美配合,并且JSON文档已发布/存储到我的MongoDB中。 The issue is when I try to login. 问题是当我尝试登录时。 Upon posting, I get the server error: 发布后,出现服务器错误:

Element does not match any field or property of class Models.ApplicationUser 元素与Models.ApplicationUser类的任何字段或属性都不匹配

The error goes away when I put all of model A's elements into ApplicationUser; 当我将模型A的所有元素都放入ApplicationUser时,错误消失了。 however, this leaves the issue of model B not being accounted for. 但是,这使得模型B的问题没有得到解决。 As such, I wanted to ask what the best path would be for me to take with this? 因此,我想问一问我采取的最佳途径是什么? Some ideas I was batting around involved creating two unique UserManagers, using one large JSON document with all elements (seems kinda unorganized), or perhaps using one UserManager with an adapter for the two models? 我想到的一些想法涉及创建两个唯一的UserManager,使用一个包含所有元素的大型JSON文档(似乎有点杂乱无章),或者使用一个UserManager和两个模型的适配器?

The way you've set this up is fine, and the way it should be. 设置的方式很好,应该如此。 The problem seems to be in how Mongo handles the inheritance, although you'd still have a different but equally problematic issue with something like SQL Server. 问题似乎出在Mongo如何处理继承上,尽管对于SQL Server之类的东西您仍然会有另一个不同但同样成问题的问题。

In short, what seems to be happening here is that you're essentially upcasting UserA / UserB to ApplicationUser , but since the JSON includes properties from those derived classes, which ApplicationUser doesn't, Mongo is choking on it. 简而言之,这里发生的事情实际上是您将UserA / UserBApplicationUser ,但是由于JSON包含那些派生类的属性,而ApplicationUser没有,所以Mongo对此感到窒息。 The solution is to use multiple UserManager s. 解决方案是使用多个UserManager It's a generic type that takes a TUser type param. 这是一个通用类型,需要一个TUser类型参数。 The default implementation is UserManager<ApplicationUser> , but that assumes everything is an ApplicationUser . 默认实现为UserManager<ApplicationUser> ,但假定所有内容均为ApplicationUser If you have derived user types, then you need to use type-specific UserManager<TUser> instances for those, ie UserManager<UserA> and UserManager<UserB> . 如果已派生用户类型,则需要为其使用特定于类型的UserManager<TUser>实例,即UserManager<UserA>UserManager<UserB>

FWIW, the problem you'd experience with another data store is that the type that is saved is what determines the value of the Discriminator column added to instantiate the right type with queries. FWIW,您在另一个数据存储区上遇到的问题是,保存的类型决定了添加的Discriminator列的值,以使用查询实例化正确的类型。 Even if you're creating UserA , for example, if you use an instance of UserManager<ApplicationUser> , it will be upcast to ApplicationUser and saved as ApplicationUser , not UserA . 例如,即使您正在创建UserA ,如果您使用UserManager<ApplicationUser>的实例,它也会被上载到ApplicationUser另存ApplicationUser ,而不是UserA This is similar to what's happening in your Mongo setup, but something like SQL Server would accept it happily (although your data would then be messed up), whereas Mongo is erroring out. 这类似于您在Mongo设置中发生的事情,但是类似SQL Server的东西会很乐意接受(尽管您的数据随后会被弄乱),而Mongo却在出错。

Long and short, always use a UserManager<TUser> instance that's specific to the user type you're working with. 总而言之,请始终使用特定于您正在使用的用户类型的UserManager<TUser>实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM