【问题标题】:Convert role in application to groupsid将应用程序中的角色转换为 groupid
【发布时间】:2013-12-11 11:06:58
【问题描述】:

在我正在处理的一个 ASP.NET 项目中,我有几个角色由管理员在数据库中设置。我必须通过用户主体声明来比较这些角色。

目前我正在将所有 GroupSID 转换为相应的名称:

PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, IdentityType.Sid, sid);

if (_roleNames.Exists(r => string.CompareOrdinal(r, group.SamAccountName) == 0))
{
    groupName = group.SamAccountName;
    return true;
}

_roleNames 是包含角色的字符串列表。

_roleNames.Add("Read");
_roleNames.Add("Edit");
_roleNames.Add("Review");
_roleNames.Add("Publish");

问题是这个过程非常缓慢。主体来自 Active Directory,并且有很多声明。

有没有办法将我的应用程序中的角色名称转换为GroupSID,这样我基本上可以跳过将GroupSID 转换为他们的名字的过程?

伪代码:

list<string> roleSidList

foreach role in roleList
   roleSidList.add(role.ConvertToSid())

foreach claim in Principal.Claims
   if(roleSidList.Exists(r => r == claim)) 
      // role exists, do something with it

【问题讨论】:

    标签: asp.net asp.net-mvc-4 windows-authentication claims


    【解决方案1】:

    解决了我的问题如下:

    我没有将 SID 传递给 GroupPrincipal,而是像这样传递 groupName:

    public bool IsUserInGroup(string groupName)
    {
        PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
        GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, IdentityType.Name, groupName);
    
        if (group == null)
            return false;
    
        return true;
    }
    

    【讨论】:

      猜你喜欢
      • 2017-01-04
      • 2020-03-03
      • 2017-12-08
      • 2013-09-28
      • 1970-01-01
      • 2013-02-07
      • 2011-07-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多