如何在 .NET 中使用成员身份重命名角色?
我正在使用 ASP.NET 会员资格,并注意到 角色类修改角色(例如其名称),仅用于创建和删除它们。
可以还是不支持?
编辑:@CheGueVerra:是的,很好的解决方法。
你知道(额外加分吗:))为什么这是不可能的?
I'm using ASP.NET Membership and noticed there isn't a method in the Roles class to modify a role (its name for instance), only to create and delete them.
Is it possible or it's not supported?
EDIT: @CheGueVerra: Yes, nice workaround.
Do you know (for extra credit :) ) why it's not possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有直接的方法可以更改会员资格提供程序中的角色名称。
我将获取要重命名的角色中的用户列表,然后从列表中删除它们,删除角色,使用新名称创建角色,然后将之前找到的用户添加到使用新名称的角色中。
这将更改角色中所有用户的角色名称。
后续:角色用于确保用户在系统中只扮演他的角色,因此 User.IsInRole(ROLE_NAME) 将帮助您强制执行适用于用户及其所在角色的 BR 安全措施。如果您可以动态更改角色名称,您将如何验证用户确实处于该角色中。 嗯,当我问起这个问题时,我就是这么理解的。
rtpHarry 编辑:将伪代码示例转换为可编译的 C# 方法
There is no direct way to change a role name in the Membership provider.
I would get the list of users that are in the role you want to rename, then remove them from the list, delete the role, create the role with the new name and then Add the users found earlier to the role with the new name.
That will change the name of the role for all users in the role.
Follow-up: Roles, are used to ensure a user plays only his part in the system, thus User.IsInRole(ROLE_NAME), will help you enforce the BR securities that apply, for a user and the roles he is in. If you can change the role names on the fly, how are you going to validate that the user is really in that role. Well that's what I understood, when I asked about it.
rtpHarry edit: Converted pseudocode sample to compilable c# method
以编程方式重命名 ASP.NET 成员资格模型中的角色将是一件坏事™,因为角色名称在配置文件中用于定义权限。 如果有一种编程方式来更改角色名称(将更改保留到数据库),您将立即破坏 web.config 中任何使用该数据库的 Web 应用程序的基于角色的安全配置,并且没有办法以保证单个 Web 应用程序可以更改使用该成员资格数据库的每个 Web 应用程序的配置。
Renaming a Role in the ASP.NET Membership model programatically would be a Bad Thing™, because the Role names are used in the configuration file to define permissions. If there were a programmatic way to change the Role name (which persisted the change to the database), you would immediately break any Role-based security configurations in web.config for any web apps using the database, and there'd be no way to guarantee that a single web app could alter the configuration of every web app using that Membership DB.