c# - DNN 检查用户是否在角色组中

标签 c# dotnetnuke roles

我在我的 DNN 模块中添加了一项检查,以排除某些用户在登录时不必回答某些问题。我不想对每个单独的角色进行硬编码,而只是排除特定角色组中的任何人。这样,如果我们将来有更多角色,如果我们希望将它们排除在外,我们可以将它们添加到角色组中。

但是,我不知道您如何检查用户是否在角色组中。我知道如何检查角色,但如果他们在一个组中则不知道。

解决方案:这是我根据得到的答案整理的代码。应该可以。

RoleGroupInfo RoleGrp = RoleController.GetRoleGroupByName(this.PortalId, "Role Group");
bool bShouldSkipQuestions = false;
if (RoleGrp != null)
{
    Dictionary<string, RoleInfo> GroupChk = RoleGrp.Roles;
    if (GroupChk.Count > 0)
    {
        foreach (var item in GroupChk.Values)
        {
            if (_user.IsInRole(item.RoleName))
            {
                bShouldSkipQuestions = true;
                break;
            }
        }
    }
}

最佳答案

角色组并不是真的打算像那样使用(它们只是为了最终用户组织),所以没有直接的方法来检查它。您需要获取组中的所有角色 (RoleController.GetRolesByRoleGroup),然后检查 PortalSecurity.IsInRoles,传入以逗号分隔的角色名称字符串.

试试这段代码:

var roleGroup = RoleController.GetRoleGroupByName(this.PortalId, "Role Group");
var shouldSkipQuestions = roleGroup != null 
                          && roleGroup.Roles.Keys.Any(role => _user.IsInRole(role));

关于c# - DNN 检查用户是否在角色组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11722765/

相关文章:

c# - 如何通过套接字从 UWP 应用程序连接到 Unity 游戏服务器套接字?

web - 开发 DotNetNuke CMS 网站

c# - DotNetNuke 升级 Telerik 错误

c# - 使用 SDK (C#) 示例创建逻辑应用工作流

c# - C# 中的 Scala flatMap.toMap 类似物

python - 如何使用tensorflow保存DNN模型

Symfony2 : How to login using OAuth (HWIOAuthBundle) + custom roles (by default and loaded from DB)

android - 来自谷歌服务的实时多人游戏的问题(在你的房间配置中添加一个独占位掩码)

spring security core 安全自定义url

c# - 使用 DLL 基地址读取内存