asp.net ProfileProvider 中基于角色的配置文件数据

发布于 2024-07-30 02:46:13 字数 256 浏览 5 评论 0原文

我在我的 asp.net 网站中定义了几个不同的角色:管理员、默认用户。 目前,我使用内置的个人资料提供程序来存储有关用户的一些信息(名字、姓氏、头像...)。 接下来我想做的是提供一个具有“管理员”角色的用户来存储一些有关他的偏好的数据。 (可能是编辑器类型、通知、日志大小...)。

可以使用默认的配置文件提供程序来完成此操作吗? 或者我是否必须构建自定义表或自定义提供程序并自己维护这些数据? 我想要的是基于角色的配置文件属性。 欢迎任何想法!

I have defined a couple of different roles in my asp.net website : Administrators, DefaultUsers. Currently i use the built-in profile provider to store some information about the user (first name, last name, avatar ...).
What i would like to do next is provide a user who has the "Admistrators" role to store some data about his preferences. (maybe EditorType, Notifications,LogSize ...).

Is it something that can be done with the default Profile provider? or do i have to build a custom table or a custom provider and maintain these data myself?
What i would like to have are role-based profile properties. Any ideas are welcome!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烛影斜 2024-08-06 02:46:13

您想要实现的目标是不可能的,因为一个配置文件绑定到一个用户而不是角色。 用户可以拥有多个角色,但不能拥有多个配置文件。

如果您无论如何都想这样做,您可以继续创建可供所有用户使用的属性,但仅对管理员中的用户有效。 仅角色。 您可以在代码中执行此检查。

我建议您在这种情况下不要使用 ProfileProviders。 在这种情况下,所有内容都存储为文本,这将严重降低应用程序性能。

What you want to achieve is not possible since one profile binds to one user and not to role. Users can have multiple roles but not multiple profiles.

If you want to do it anyway, you can go on creating properties available to all users but effective only to those in Admin. role only. You can do this check in code.

I recommend you not to use ProfileProviders in this scenario. Everything is stored as text in this case which will deteriorate the application performance heavily.

爱情眠于流年 2024-08-06 02:46:13

使用默认的配置文件提供程序,您可以添加这些额外的仅供管理的属性。 当然,所有用户都可以使用它们,但通过代码控制它很简单。

作为一个额外的优势,如果某人的管理员权限被暂时撤销,信息仍然会保存,并且当他们取回时,所有旧值仍然存在。 当然,您可以根据自己的需要清除或不清除它们。

与任何安全系统一样,用户特定属性的存在不应推断角色/组成员资格,因此请确保您仍然执行 user.IsInRole("Administrators") 检查。

With the default Profile provider you can add those extra admin-only properties. Sure they'll be available to all users, but it's simple enough to control that from code.

As an added advantage if someone has their admin privileges temporarily revoked the information is still saved, and when they get them back all their old values are still there. Of course you can clear them or not depending on your requirements.

As with any security system, the presence of a particular property on a user should not infer role/group membership, so make sure you still do your user.IsInRole("Administrators") checks.

满栀 2024-08-06 02:46:13

正如其他回答者已经指出的那样,您无法真正扩展内置的基于角色的系统。

然而,为了保留当前的 ​​ASP.NET 成员资格和角色提供者并继续从他们的服务中受益,您可以做的只是简单地为 aspnet_Roles 创建一个扩展表。 像这样的事情:

CREATE TABLE dbo.MyOwnRoleExtensions
(ExtensionID INT IDENTITY(1,1) PRIMARY KEY,
 ApplicationId uniqueidentifier,
 RoleId uniqueidentifier,
 (whatever fields you need here......)
)

然后,您可以在 (ApplicationId, RoleId) 上建立与 aspnet_Roles 表的外键关系,从而将您的“角色扩展”表与基本 aspnet_Roles 表相关联。

当然,如果您需要检索扩展表中的其他字段,则需要在自己的代码中执行此操作 - 但这样,您仍然可以使用和利用内置 ASP.NET 成员资格的所有优点和角色提供者,你不需要任何肮脏的黑客或任何东西。

马克

You can't really extend the built-in role-based system, as the other answerer have already noted.

What you could do, however, in order to keep your current ASP.NET membership- and role-providers and keep benefitting from their services, is just simply create an extension table for the aspnet_Roles. Something like this:

CREATE TABLE dbo.MyOwnRoleExtensions
(ExtensionID INT IDENTITY(1,1) PRIMARY KEY,
 ApplicationId uniqueidentifier,
 RoleId uniqueidentifier,
 (whatever fields you need here......)
)

You could then establish a foreign key relationship to the aspnet_Roles table on (ApplicationId, RoleId) and thus associate your "role extensions" table with the base aspnet_Roles table.

Of course, if you need to retrieve the additional fields in your extensions table, you'll need to do that in your own code - but that way, you can still use and leverage all the niceties of the built-in ASP.NET membership and role providers and you don't need any dirty hack or anything.

Marc

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文