如何使用.NET中的成员资格重命名角色? [英] How do you rename a Role using Membership in .NET?

查看:69
本文介绍了如何使用.NET中的成员资格重命名角色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ASP.NET Membership,并注意到

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.

有可能还是不受支持?

@CheGueVerra:是的,不错的解决方法.

@CheGueVerra: Yes, nice workaround.

您是否知道(要获得额外的信用:))为什么不可能?

Do you know (for extra credit :) ) why it's not possible?

推荐答案

在成员资格提供程序中没有直接更改角色名称的方法.

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.

public void RenameRoleAndUsers(string OldRoleName, string NewRoleName)
{
    string[] users = Roles.GetUsersInRole(OldRoleName);
    Roles.CreateRole(NewRoleName);
    Roles.AddUsersToRole(users, NewRoleName);
    Roles.RemoveUsersFromRole(users, OldRoleName);
    Roles.DeleteRole(OldRoleName);
}

这将更改角色中所有用户的角色名称.

That will change the name of the role for all users in the role.

后续跟踪:角色,用于确保用户仅在系统中扮演其角色,因此User.IsInRole(ROLE_NAME)将帮助您强制实施适用于用户及其角色的BR证券.如果您可以即时更改角色名称,则如何验证用户确实在该角色中.嗯,当我问到这一点时,我就是这么理解的.

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将伪代码样本转换为可编译的c#方法

rtpHarry edit: Converted pseudocode sample to compilable c# method

这篇关于如何使用.NET中的成员资格重命名角色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆