ASP.NET 中可以为不同的用户提供多个配置文件吗?
我的应用程序有几种不同的用户类型及其自己的成员。例如,我有学生用户和教师用户。我通过自定义 MembershipProvider 的 ValidateUser 方法中的活动目录对我的用户进行身份验证。在查询AD的同时,我拉出了他们所有的相关信息。我想将该信息放入配置文件中,但从我读到的内容和我发送的示例来看,您只能定义一个配置文件(如下所示):
<profile defaultProvider="CustomProfileProvider" enabled="true">
<properties>
<add name="YearLevel" type="String" />
<add name="Name" type="String" />
<add name="Age" type="String" />
</properties>
</profile>
上面的配置文件适用于学生,但不适用于学生对于在 AD 中没有“YearLevel”值的教师。是否可以使用多个配置文件来实现此目的?或者从 AD 中添加涵盖所有用户类型的所有属性是否更容易,然后在我的代码中,只需检查它们是什么用户类型,然后访问其特定属性?
My application has a few different user types with their own members. For example, I have the student user as well as the teacher user. I authenticate my users through active directory in my custom MembershipProvider's ValidateUser method. While querying AD, I pull out all their relevant information. I'd like to put that information into a Profile, but from what I've read and the examples I've sen, you can only define a single Profile (like so):
<profile defaultProvider="CustomProfileProvider" enabled="true">
<properties>
<add name="YearLevel" type="String" />
<add name="Name" type="String" />
<add name="Age" type="String" />
</properties>
</profile>
The above profile would work for a student, but not for a teacher, who does not have a value for "YearLevel" in AD. Is it possible to have multiple profiles to accomplish this? Or is it easier to add all properties from AD spanning all user types, then in my code, just check what user type they are and then access their specific properties?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您实际上只有 2 个选项:
您可以创建一个包含所有用户类型字段的“大”配置文件,然后仅访问您当前使用的用户类型的字段。 (我不建议这样做,但这可能是一个快速解决方案)。
实现自定义配置文件提供程序 - 这一点也不难。您可以在此处了解有关此内容的更多信息: http://msdn.microsoft.com/ en-us/library/0580x1f5.aspx
You really only have 2 options here:
You can just create a "big" profile with fields for all user types and then only access the fields for the user type you are currently using. (I don't recommend this, but it could be a quick fix).
Implement a custom profile provider - which isn't that hard at all. You can learn more about this here: http://msdn.microsoft.com/en-us/library/0580x1f5.aspx
编辑2015-01-26:嗯...我刚刚提高了1,但我实际上认为不应该这样。现在,我重读了这个问题和我自己在两年多前的回答,我实际上不认为配置文件组是启用多个配置文件“类型”的方式。您也许可以将两种不同类型的属性填充到两个不同的组中(至少是“正交”属性),但您仍然需要代码来区分这两者。所以我会选择 apiguy 或 Jim Schubert 的答案 ENDEDIT
这不是 个人资料组的用途?喜欢:
EDIT 2015-01-26: Hmm... my just got upped 1, but I actually think it shouldn't have been. Now that I reread the question and my own answer of over 2 years ago I actually DON'T think profile groups are the way to enable multiple profile 'types'. You could perhaps stuff in the properties of two different types into two different groups (at least 'orthogonal' properties), but you still need code to distinguish the two. So I would go with apiguy or Jim Schubert's answer ENDEDIT
Isn't this what profile groups are for? Like:
是的,如果您要使用配置文件模型,恐怕您必须指定所有可能的属性。如果您走这条路,我建议使用某种代理,该代理将接受用户并根据其类型填充适当的属性。
就像
我会考虑使用两个不同的表来将两个对象分开,或者实现一个自定义配置文件提供程序。
Yeah, I'm afraid you would have to specify all possible properties if you are going to be using profile model. If you go that route, I would suggest using some sort of proxy that would take a user and based on it's type populate the appropriate properties.
something like
I would consider having two different tables though that keep the two objects separate, or implement a custom profile provider.
您可以创建一个对象并将其存储在配置文件中。
例如:
在 web.config 中:
编辑:我不记得是否可以使用接口作为类型。将其更改为对象。
通过 Profile.UserProfile 访问它(冗余,但它有效)。
然后,要处理这个问题,您必须检查类型:
您还可以在配置文件对象中存储泛型(也许是字典?以下是我使用过的实现)
例如:
在 web.config 中:
我使用了这个.NET 3.5 中的方法,我不确定它是否适用于 .NET 2 或 3。我假设泛型以相同的方式处理,因为编译器没有改变。
笔记:
有必要将通用对象继承为空对象,因为配置文件设置不喜欢以下内容:
上面是完全限定的 IL 名称,因为它确实应该看起来。 XML 似乎不喜欢刻度线。
我还没有研究过在 Profile 对象中存储序列化对象的任何性能问题,因此我不会建议您经常需要的任何属性都这样做。
You could create an object and store that in the Profile.
For instance:
In web.config:
Edit: I can't remember if you can use an interface as the type or not. Changed it to object.
Access this via Profile.UserProfile (redundant, but it works).
Then, to process this, you'll have to check the type:
You could also store generics in the profile object (Perhaps Dictionary? The following is an implementation I've used)
For instance:
And in web.config:
I used this method in .NET 3.5, I'm not sure if it works in .NET 2 or 3. I would assume the generics are processed the same way, since the compiler hasn't changed.
Note:
It is necessary to inherit the generic object into an empty object, because the profile setting doesn't like the following:
The above is the fully-qualified IL name as it really should look. The XML doesn't like the tick mark, it seems.
I haven't researched any performance issues with storing serialized objects in the Profile object, and therefore I wouldn't recommend this for any properties you'll need on a fairly regular basis.